简体   繁体   中英

Sorting array with NSSortDescriptor for specific item

I am using an NSSortDescriptor to sort an array as follows:

NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects: titleDescriptor, nil]; 

self.items = (NSMutableArray*)[self.items sortedArrayUsingDescriptors:sortDescriptors];

Which works great. What I would like to do is have one specific "title" (from which I know the exact string) always at the top of the list, and then have the NSSortDescriptor 's sort the rest of the titles alphabetically below said title. What is the syntax to accomplish this using NSSortDescriptor ?

You should use one of the other methods like sortedArrayUsingSelector: to gain this level of control.

Also, don't just cast the result to a mutable array. If the array is mutable, use the method which sorts the mutable array. If you use the method to create a new array, make sure it is mutable by calling mutableCopy on the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM