I have the following sort descriptors that sort an array of my business objects, ready to be displayed in a table, I’m starting off with some sample sorting code from a previous SO question
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"awardedOn" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
NSArray *sortedArray = [returnable sortedArrayUsingDescriptors:sortDescriptors];
The objects that I’m displaying all will have a title. Only some of them will have an “awardedOn” set, which is an NSDate.
What I want to do:
- Sort entire array so all the objects with an “awardedOn” set are
displayed at the top - Within the two “sets”, order them alphabetically
- I don’t care about the actual value of the date, I’m more interested
if it exists or not
Something like this (Titles, the bold ones have a value for awardedOn)
- Awesome
- Better
- Cool
- Another
- Another One
- One more
- Yet Another
You should be able to do that using two descriptors like you first said, first by awardedOn, then by title. However, you need to provide a custom NSSortDescriptor for the awardedOn sort that looks someting like this:
This will allow you to have to separate sets: Awesome/Better/Cool and Another/Another One/One More/Yet another, in your example. After that, you should be good with:
On a final note, you might need a litte more work depending on what your “empty” awardedOn fields look like (I assumed, in the code above, that the field was set to null). You can take a look here: https://stackoverflow.com/a/3145789