3

Consider this array. It has no keys, so I am not sure I can use NSSortDescriptor. What would be the best method to sort them?

(
    "Thursday, July 30, 2009",
    "Monday, September 07, 2009",
    "Wednesday, September 09, 2009",
    "Friday, August 14, 2009",
    "Saturday, September 05, 2009",
    "Monday, August 10, 2009",
    "Thursday, July 23, 2009",
    "Monday, October 12, 2009",
    "Friday, October 16, 2009",
    "Monday, August 17, 2009",
    "Tuesday, October 13, 2009",
    "Wednesday, September 30, 2009",
    "Sunday, August 16, 2009",
    "Thursday, August 27, 2009",
    "Monday, August 31, 2009",
    "Saturday, August 15, 2009",
    "Thursday, August 06, 2009",
    "Saturday, September 26, 2009",
    "Tuesday, September 29, 2009",
    "Tuesday, September 15, 2009",
    "Tuesday, September 01, 2009"
)
Matt Lacey
  • 8,227
  • 35
  • 58
cvb
  • 1,715
  • 18
  • 22

2 Answers2

12

You can use sortedArrayUsingFunction:

Here's some example code.

NSInteger dateSort(id s1, id s2, void *context)
{
    NSDateFormatter* format = (NSDateFormatter*)context;
    NSDate* d1 = [format dateFromString:s1];
    NSDate* d2 = [format dateFromString:s2];

    return [d1 compare:d2];
}

...

-(void)someObjectiveCMethod
{
    NSDateFormatter* format = [[[NSDateFormatter alloc] init] autorelease];

    // Find theses format codes here:
    // http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
    //
    [format setDateFormat:@"EEEE, MMMM dd, yyyy"];

    NSArray* myArray = getMyArray();
    NSArray* sortedArray = [myArray sortedArrayUsingFunction:dateSort context:format];
}

There are a number of these sorts of sort methods in NSArray. It's worth looking at those to familiarize yourself with them.

nall
  • 15,899
  • 4
  • 61
  • 65
  • Hmmm...this didn't actually sort the list. Will dig with it. – cvb Oct 23 '09 at 15:30
  • 2
    For efficiency's sake, I'd argue for creating the NSDateFormatter in someObjectiveCMethod, and passing it as the context param. Or, at very least, not using autorelease in dateSort. – Sixten Otto Oct 23 '09 at 15:35
  • I also changed to pass the formatter via context. I was just trying to be explicit about things, but Sixten is correct. You wouldn't have it there in real code. – nall Oct 23 '09 at 15:48
3
NSArray * unsortedDates = ...;
NSArray * sortedDates = [unsortedDates sortedArrayUsingSelector:@selector(compare:)];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Oh, I was under the impression the array had NSStrings, not NSDates. If it has NSDates, Dave's answer is the right one (less code is good). – nall Oct 23 '09 at 14:45
  • Oh hey, I didn't even think they might be strings. I just assumed that an array of date-looking objects would be dates, because it'd be pretty crazy to store dates any other way. – Dave DeLong Oct 23 '09 at 14:57
  • Yes, sorry, they are strings not nsdates. – cvb Oct 23 '09 at 15:19
  • I'd edit the title and/or content to make that more apparent. :) – Wevah Oct 23 '09 at 20:55
  • I would suggest making them dates. It's good to keep your date-parsing code and your date-sorting code (separate tasks) in separate places. – Peter Hosey Oct 24 '09 at 01:30