2017-03-05 12 views
-1

この並べ替えの方法配列には日付と時刻が含まれています。同じ日付で並べ替える必要があります複数のエントリーを日付順に並べ替える必要があり、すべてのエントリーをドレッシング順に表示します。日付を「@ dd/MM/yyyy hh:mm a」に降順で並べ替える方法

data= { 
    { 
      dateAndTime = "04/03/2017 09:52 PM"; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     }, 
      { 
      dateAndTime = "02/03/2017 06:54 PM"; 
      deviceImage = ""; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     }, 
      { 
      dateAndTime = "04/03/2017 05:36 PM"; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     }, 
      { 
      dateAndTime = "03/03/2017 04:48 PM"; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     }, { 
      dateAndTime = "03/03/2017 06:48 PM"; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     },{ 
      dateAndTime = "02/03/2017 09:36 PM"; 
      deviceImage = ""; 
      dialOrReceive = 0; 
      number = "+918839752179"; 
      profilePhoto = ""; 
     }, 
} 
+0

どれでも試みを試してみてください、何をしようとしたのですか?コードスニペットを投稿してください –

+0

キー 'data'のオブジェクトは辞書ではなく配列であるようです。配列をソートするには、日付文字列をNSDateにマップする必要があります。 – vadian

+0

私は日付を正常にソートしています:NSSortDescriptor * sortOrder = [NSSortDescriptor sortDescriptorWithKey:@ "dateAndTime"昇順:NO];arr = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortOrder]]; –

答えて

1

この

- (void)sortDict { 

    NSDictionary *dict = @{ 
          @"data" : @[@{ 
              @"dateAndTime" : @"04/03/2017 09:52 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"02/03/2017 06:54 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"04/03/2017 05:36 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"03/03/2017 04:48 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"03/03/2017 06:48 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"02/03/2017 09:36 PM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              }, 
             @{ 
              @"dateAndTime" : @"02/03/2017 07:36 AM", 
              @"deviceImage" : @"", 
              @"dialOrReceive" : @0, 
              @"number" : @"+918839752179", 
              @"profilePhoto" : @"" 
              } 
             ] 
          }; 

    NSDateFormatter *formatter = [NSDateFormatter new]; 
    formatter.dateFormat = @"dd/MM/yyyy hh:mm a"; 

    NSArray *arr = [dict objectForKey:@"data"]; 

    arr = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { 

     NSDate *date1 = [formatter dateFromString:[obj1 objectForKey:@"dateAndTime"]]; 
     NSDate *date2 = [formatter dateFromString:[obj2 objectForKey:@"dateAndTime"]]; 

     return [date2 compare:date1]; 
    }]; 

    NSLog(@"%@", arr); 
} 
+0

ありがとうございました@ schmidt9 –

+0

ありがとうございます@ schmidt9 –

+0

@kumar正確には動作しないものは – schmidt9

関連する問題