I am trying to find an easy way to sort NSMutableArray that contains NSDictionaries. Each NSDictionary has a key named "Date" that contains the NSString of the date (for example: 10/15/2014).
What I want to do is sort the array based on these rows in ascending order.
I tried this with no luck:
NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM/dd/yy"]; NSDate *d1 = [formatter dateFromString:s1]; NSDate *d2 = [formatter dateFromString:s2]; return [d1 compare:d2];
I also tried this with no luck:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES]; stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]]; recent = [stories copy];
Each method crashes, and I think this is because it is NSString instead of NSDate, but I just don't understand how to do it.
Can someone show me the correct way to do this?
This is how I call the first block of code:
theDictionaries = [[theDictionaries sortedArrayUsingFunction:dateSort context:nil] mutableCopy];
source share