IOS: natural sort order
1 answer
You can use localizedStandardCompare: as a selector in a sort descriptor to request a Core Data fetch like
NSSortDescriptor *titleSort = [[NSSortDescriptor alloc] initWithKey:@"title"
ascending:YES
selector:@selector(localizedStandardCompare:)];
[fetchRequest setSortDescriptors:[titleSort]];
Swift 3:
let titleSort = NSSortDescriptor(key: "title",
ascending: true,
selector: #selector(NSString.localizedStandardCompare))
fetchRequest.sortDescriptors = [sortDescriptor]
or better
let titleSort = NSSortDescriptor(key: #keyPath(Entity.title),
ascending: true,
selector: #selector(NSString.localizedStandardCompare))
fetchRequest.sortDescriptors = [sortDescriptor]
where "Entity" is the name of the subclass of the managed data object.
+6
source to share