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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.