简体   繁体   中英

NSSortDescriptor - has the order changed?

I have an NSFetchRequest that is getting objects from core-data and sorting them using a trivial NSSortDescriptor . I use a couple of different ones depending on what the user selects, but they are like this:

    NSMutableArray *sortDescriptors = [NSMutableArray array];
    NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"ownerName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"petName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [sortDescriptors addObject:sortDescriptor1];
    [sortDescriptors addObject:sortDescriptor2];
    [fetchRequest setSortDescriptors:sortDescriptors];

Everything works as expected on iOS5 and 5.1, however when I test exactly the same code on an iPad running iOS4.2.1 (or the 4.3 simulator) the order of all the first sort descriptor is reversed. In this example, the owners names are ordered Z to A, but the pets are correctly ordered A to Z!

Was there a bug in earlier iOS versions that caused the ascending boolean to be the wrong way around? Alternatively, is there something else I may be doing incorrectly?

EDIT: The first key (ownerName) is used as the NSFetchedResultsController section. Not sure if this is relevant.

Thanks Craig

Just guessing.

I have had good results without specifying caseInsensitiveCompare: .

Also, strictly speaking NSFetchRequest expects an NSArray as the sortDescriptors argument, not NSMutableArray . In all of Apple's sample code it is normally something like

[fetchRequest setSortDescriptors:
   [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];

I have had errors like that (using mutable instead of immutable), but often it also just works. Maybe changing that is worth a try.

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