How to establish secondary NSSortDescriptor sort key?
Asked Answered
C

2

11

I have successfully sorted the data I have by my sort key lastName, but I want to know how to sort by lastName, and then by firstName. Here is the code I used to sort by lastName

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

How do I add the secondary sort key of firstName?

Cory answered 7/12, 2011 at 5:4 Comment(0)
S
47
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];

[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];
Soothsay answered 7/12, 2011 at 5:7 Comment(1)
The solution is gone @tarheel. The link is dead.Florri
D
0

Notice that you are passing an array of sort descriptors. Simply create another descriptor for firstname and create the array with both descriptors. They will be applied in the order of the array.

Diaphane answered 7/12, 2011 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.