Reputation: 193
here I try to sort a NSdictionary key and a value I find a method but it does not work.
[detailEvent[@"occurences"] sortUsingSelector : @selector(compare:)];
here is the source sorted
"occurences": [
{
"jour": "2014-07-27T00:00:00.000Z",
"hour_start": "11:00:00",
"hour_end": "18:00:00"
},
{
"jour": "2014-07-26T00:00:00.000Z",
"hour_start": "11:00:00",
"hour_end": "18:00:00"
},
{
"jour": "2014-07-25T00:00:00.000Z",
"hour_start": "11:00:00",
"hour_end": "18:00:00"
}
I do not add all because it is very long
I want sorted by date so the key jour
is that there is another method to sort the dictionary?
thanks
Upvotes: 0
Views: 73
Reputation: 34829
I believe that this will sort the dictionaries correctly
NSArray *sorted = [detailEvent[@"occurences"] sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *d1, NSDictionary *d2)
{
NSString *s1 = d1[@"jour"];
NSString *s2 = d2[@"jour"];
return( [s1 compare:s2] );
}];
If you want them sorted so the most recent date is first, then change the order of the compare
return( [s2 compare:s1] );
Upvotes: 2