Josh Sherick
Josh Sherick

Reputation: 2161

Multiple sort descriptors

When specifying multiple sort descriptors:

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastName, firstName, age, nil];

Say lastName, firstName and age are all of type NSSortDescriptor and have keys suggested by their names.

I just want to understand what will happen when I do this. Say I have some core data (list of people, for example) and I sort it using these sort descriptors. Will it try to sort the last names first, then iff the last names are the same, try to sort (just those records where the last name is the same) by the first names, then iff the first names and last names are the same, it will try to sort (just for those records) by age as a last resort. Or will it sort the list in order of last name, then go back and sort it again by first name, then go back again and sort it by age?

Upvotes: 0

Views: 787

Answers (1)

Duncan Babbage
Duncan Babbage

Reputation: 20187

It does what you would want—the first one. I doubt the technical implementation is as you describe it but the result is the same.

One way the technical implementation could be achieved is just to run a straight sort for each, but run them in reverse order (tertiary sort, followed by secondary sort, followed by primary sort). This will have the net effect of the outcome you're wanting, and doesn't require each sort to take any notice of the other sorts, but produces the sort on the primary key, sub-ordered by the secondary key, sub-ordered again by the tertiary key. But, there may be more efficient implementations of the sort again that the database is able to employ. The good news is you don't need to know how it works—it just does. :)

Upvotes: 6

Related Questions