Reputation: 7
I have array
of dictionaries
, i want to sort the array
using the key
@"date" which is in the format of a dictionary
like.
"date": {
"day": 19,
"month": 5,
"year": 2014
},
how can i sort the array based on the key
@"date"??
Thanks
Upvotes: 0
Views: 490
Reputation: 1405
Try using
NSArray *sortedArray = [myArray sortedArrayUsingComparator:^(id o1, id o2) {
NSDictionary *dict1 = (NSDictionary *)o1;
NSDictionary *dict2 = (NSDictionary *)o2;
if (![dict1[@"date"][@"year"] isEqualToString:dict2[@"date"][@"year"]])
return [dict1[@"date"][@"year"] compare:dict2[@"date"][@"year"] options:NSCaseInsensitiveSearch];
else {
// years are equal
// continue comparing by month, day, etc
}
}];
Only compare based on the keys you want
Upvotes: 1