Reputation: 12444
Currently in my app I have a NSArray that I load from NSUserDefaults. Each object in the NSArray is a NSDictionary that has about 5 different keys. I am re-order the NSDictionarys themselves based upon the NSDates inside of them. I do not want to sort the NSDates alone.
I have tried to do this with this code:
NSComparator sortByDate = ^(id dict1, id dict2) {
NSDate* n1 = [dict1 objectForKey:@"Date"];
NSDate* n2 = [dict2 objectForKey:@"Date"];
return (NSComparisonResult)[n1 compare:n2];
};
[self.cellArray sortUsingComparator:sortByDate];
Although nothing happens even though the code gets executed. I am confused though about how to re-order the NSDictionarys based upon something inside of it.
Can anyone offer any insight on this?
Thanks!
Upvotes: 1
Views: 985
Reputation: 18551
You could try sorting it inline but I doubt that would have any major effect. But I can say i never abstract out my blocks. I would definitely try NSLogging too to make sure you're getting valid NSDates.
[cells sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate *n1 = [obj1 objectForKey:@"Date"];
NSDate *n2 = [obj2 objectForKey:@"Date"];
if (n1 > n2) {
return NSOrderedAscending;
} else (n1 < n2) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}];
Upvotes: 1