Skip to content

Instantly share code, notes, and snippets.

@egold
Created April 3, 2012 19:15
Show Gist options
  • Save egold/2294834 to your computer and use it in GitHub Desktop.
Save egold/2294834 to your computer and use it in GitHub Desktop.
Quickie NSArray sorting
NSSortDescriptor* sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"createdDate" ascending:NO];
allReminders = [allReminders sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByDate]];
self.reminders = [NSMutableArray arrayWithArray:allReminders];
@egold
Copy link
Author

egold commented Apr 3, 2012

This assumes you're sorting an array of objects that have a "createdDate" field.

Any object that conforms to Key Value Coding (KVC) such as NSManagedObjects or NSDictionaries will work with this.

So, you could use it to sort Core Data objects or JSON parsed into dictionaries for instance.

@egold
Copy link
Author

egold commented Apr 3, 2012

There are more flexible ways to sort NSArrays using blocks or custom methods, but this covers 90% of cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment