You have to build yourself a suitable comparator:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSComparator myDateComparator = ^NSComparisonResult(id obj1,
id obj2) {
NSDate *date1 = [formatter dateFromString:obj1];
NSDate *date2 = [formatter dateFromString:obj2];
return [date2 compare:date1]; // sort in descending order
};
then simply sort the array by doing:
NSArray *dateArray = @[@"01/12/2012", @"01/26/2011", @"02/09/2005",
@"02/24/2006", @"03/19/2007", @"07/14/2011",
@"08/17/2007", @"10/04/2007", @"10/31/2006",
@"12/05/2012", @"12/06/2006", @"12/23/2008"];
NSArray *sortedArray = [dateArray sortedArrayUsingComparator:myDateComparator];
NSLog(@"Sorted array is %@", sortedArray);
This sorts the strings (and not any modified array). You could also use string manipulation and convert m/d/y to y-m-d, but i guess for small data sets this approach would be fine.