Kenny Wyland
Kenny Wyland

Reputation: 21870

CoreData sorting is treating single straight quote and single smart quote as the same, how to treat them differently?

I've got a CoreData database and I'm fetching a list of Animals from it, sorted by a namesortable field. But namesortables that start with ' and with ` are being treated as if they are the same and producing results like this:

'affirm'
‘california’
'grasshopper'
'no doubt"
‘relentless’
...

This wouldn't inherently be a problem, but I'm fetching these with an NSFetchedResultsController which is sectioned by -namefirstletter and it's crashing because the sections are out of order. It thinks the sections are out of order because it keeps switching back and forth between ' and with `.

I know that I could alter the -namefirstletter method to swap smart quotes for single quotes to bludgeon it into working, but I'd rather not have to start creating a longer and longer list of exception characters that I need to swap. I'd prefer to get CoreData to just sort the data without treating ' and with ` as the same.

Any suggestions?


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:Animal.entityname inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSArray<NSSortDescriptor *> *sortdescriptors = @[
    [NSSortDescriptor sortDescriptorWithKey:Animal.fieldname_namesortable 
        ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]
];

[fetchRequest setSortDescriptors:sortdescriptors];


fetchRequest.predicate = [NSPredicate predicateWithFormat:@"..."];

NSError *error = nil;
NSArray<Animal*> *animals = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

Upvotes: 1

Views: 35

Answers (0)

Related Questions