I'm trying to sort a core data entity by the length of its 'name' property, which is a string.
Location
is a NSManagedObject
containing a name
property (string).
Here's the code:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[Location entityName]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", query]];
[fetchRequest setSortDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"name.length" ascending:YES]]];
[fetchRequest setFetchLimit:3];
NSArray *locations = [context executeFetchRequest:request error:&error];
I would expect the name.length KVO operator to work but the results are not correctly ordered.
Eg: if the model contains three Location
's which name
properties are New Orleans, New York and Newcastle, the resulting array with query
= "New" should be New York, Newcastle and New Orleans.
Am I missing anything?
Thanks, DAN