views:

5627

answers:

5

I have an NSArray that contains date strings like this: "Thu, 21 May 09 19:10:09 -0700"

I need to sort the NSArray by date. I thought about converting the date string to an NSDate object first, but got stuck there on how to sort by the NSDate object.

Thanks.

+2  A: 

Once you have an NSDate, you can create an NSSortDescriptor with initWithKey:ascending: and then use sortedArrayUsingDescriptors: to do the sorting.

smorgan
This approach will work, but using -sortedArrayUsingSelector: is a bit simpler and doesn't require creating an extra object.
Quinn Taylor
Pre-editing, the question mentioned "a 'date' key", so I assumed this was really an array of objects/dictionaries that had a date as one field, not an array of raw dates. In that case sortedArrayUsingSelector: is more complex, since it requires writing a custom sort routine to do the lookup.
smorgan
Yeah, sorry for any confusion — I cleaned up that reference since he was just saying he was storing his array with a 'date' key, and I realized it just made the question more confusing. You're definitely correct about relative complexity given a more complex data structure.
Quinn Taylor
+1  A: 

You can use sortedArrayUsingFunction:context:. Here is a sample:

NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) {
    NSDate *d1 = [NSDate dateWithString:s1];
    NSDate *d2 = [NSDate dateWithString:s2];
    return [d1 compare:d2];
}

NSArray *sorted = [unsorted sortedArrayUsingFunction:dateSort context:nil];

When using a NSMutableArray, you can use sortArrayUsingFunction:context: instead.

notnoop
This is a bad idea — a comparison function should be fast, and shouldn't have to create NSDate objects from strings for each comparison. If you're already using -[NSDate compare:], just store the dates in the array and use -sortedArrayUsingSelector: directly.
Quinn Taylor
+13  A: 

Store the dates as NSDate objects in an NS(Mutable)Array, then use -[NSArray sortedArrayUsingSelector: or -[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter. The -[NSDate compare:] method will order dates in ascending order for you. This is simpler than creating an NSSortDescriptor, and much simpler than writing your own comparison function. (NSDate objects know how to compare themselves to each other at least as efficiently as we could hope to accomplish with custom code.)

Quinn Taylor
It's not clear if the original question was asking about sorting an array of dates, or sorting an array of objects that have a date field. This is the easiest approach if it's the former, but if it's the latter, NSSortDescriptor is the way to go.
smorgan
A: 

thanks - its work

Manson
+1  A: 

If I have an NSMutableArray of objects with a field "beginDate" of type NSDate I am using an NSSortDescriptor as below:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
vinzenzweber