I have an NSArray
that contains date strings (i.e. NSString) 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.
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.)