Reputation: 943
Question is short:
I have an array of some NSManagedObjects,
all of them have an NSDate attribute
Now I want to sort this arry by their date, the latest the first,
how could implement this?
Upvotes: 0
Views: 615
Reputation: 16725
You want to use NSArray's sortedArrayUsingDescriptors:
method with an NSSortDescriptor. If your array is called array
and the NSDate attribute is called date
, then this would work:
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
Also, note that when you're getting these NSManagedObjects using an NSFetchRequest, you can give the request sortDescriptors so that they're already sorted. Using the same sortDescriptors
from above, just do the following before executing the request:
request.sortDescriptors = sortDescriptors;
Upvotes: 2