Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, December 17, 2013

iOS Setting Multiple Sort Descriptors

So, you want to have multiple NSSortDescriptors on your NSFetchRequest, huh?

So, you're using Core Data, and loving the power (and the limitations) that comes with it, and then you run up against the need to sort by two attributes in an Entity NSFetchRequest. How do you accomplish this? Well, by using an array of NSSortDescriptors, of course. But how do you us it? Well, how about this.

request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"last_name" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES], nil];

In this example, we're sorting by the last_name entity attribute, and then running a secondary sort by first_name. This will give us an order that looks something like this:

Cowen, Adam
Cowen, Paul
Cowen, Steve
Goldman, Peter
Goldman, Joseph
Welch, Barry
Welch, Joshua
Weiss, Daniel

If you need a full example of a NSFetch request with sort multiple descriptors, checkout our writeup here: Core Data NSFetchRequest with Multiple Sort Descriptors.

If you found this helpful, please consider giving us a +1, thanks!

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates