Reputation: 2534
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.
Upvotes: 87
Views: 83635
Reputation: 44769
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.)
Upvotes: 112
Reputation: 59299
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.
Upvotes: 5
Reputation: 4739
Swift 3.0
myMutableArray = myMutableArray.sorted(by: { $0.date.compare($1.date) == ComparisonResult.orderedAscending })
Upvotes: 2
Reputation: 3429
You may also use something like the following:
//Sort the array of items by date
[self.items sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj2.date compare:obj1.date];
}];
But this does assume that the date is stored as a NSDate
rather a NString
, which should be no problem to make/do. Preferably, I recommend also storing the data in it's raw format. Makes it easier to manipulate in situations like this.
Upvotes: 17
Reputation: 9649
What it worked in my case was the following:
NSArray *aUnsorted = [dataToDb allKeys]; NSArray *arrKeys = [aUnsorted sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd-MM-yyyy"]; NSDate *d1 = [df dateFromString:(NSString*) obj1]; NSDate *d2 = [df dateFromString:(NSString*) obj2]; return [d1 compare: d2]; }];
I had a dictionary, where all keys where dates in format dd-MM-yyyy. And allKeys returns the dictionary keys unsorted, and I wanted to present the data in chronological order.
Upvotes: 5
Reputation: 17
Change this
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
To
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
Just change the KEY: it must be Date
always
Upvotes: 1
Reputation: 21569
Once you have an NSDate
, you can create an NSSortDescriptor
with initWithKey:ascending:
and then use sortedArrayUsingDescriptors:
to do the sorting.
Upvotes: 4
Reputation: 3389
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];
Upvotes: 115
Reputation: 781
You can use blocks to sort in place:
sortedDatesArray = [[unsortedDatesArray sortedArrayUsingComparator: ^(id a, id b) {
NSDate *d1 = [NSDate dateWithString: s1];
NSDate *d2 = [NSDate dateWithString: s2];
return [d1 compare: d2];
}];
I suggest you convert all your strings to dates before sorting not to do the conversion more times than there are date items. Any sorting algorithm will give you more string to date conversions than the number of items in the array (sometimes substantially more)
a bit more on blocks sorting: http://sokol8.blogspot.com/2011/04/sorting-nsarray-with-blocks.html
Upvotes: 10