I am sorting results of a fetch request with a sort descriptor.
NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:[MyEntity entityName]];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"property"
ascending:YES
selector:@selector(localizedCompare:)];
req.sortDescriptors = [NSArray arrayWithObject:descriptor];
return [self.managedObjectContext executeFetchRequest:req error:nil];
The problem is the words, which begins with non-english chars like 'İ' are listed at the end of the list. It is a turkish letter and the alphabet looks like this :
A, B, C, Ç, D, E, F, G, Ğ, H, I, İ, J, K, L, M, N, O, Ö, P, R, S, Ş, T, U, Ü, V, Y, Z.
So the letter is at 12th position.
I don't know why but using comparator after fetching objects works. So it works on any array but not with sort descriptor for fetch request.
Take a look at the details of my question at NSFetchedResultsController v.s. UILocalizedIndexedCollation I show how to use the UILocalizedIndexedCollation to correctly generate the Alphabet and sort using the correct sort method based on the UILocalizedIndexCollation. My question just revolves around asking for a better way to do this.
If you dont use the UILocalizedIndexCollation you should only use the localizedStandardCompare: not localizedCompare as mentioned in the WWDC videos for localization.
Try
[NSSortDescriptor alloc] initWithKey:@"property" ascending:YES selector:@selector(localizedCompare:)]
EDIT
@Mert has updated his question. It seems now that localizedCompare:
sorts the turkish letters correctly, but does not work with the fetch request.
Here is what I have done to test this problem. Perhaps you can check if that works in your environment and then work from there:
// Create some entities:
NSArray *a = @[@"İ", @"J", @"Ğ", @"G", @"H", @"I", @"Ç", @"C"];
for (NSString *s in a) {
MyEntity *e = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity"
inManagedObjectContext:self.managedObjectContext];
e.name = s;
}
// Fetch all entities in sorted order:
NSFetchRequest* req = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES
selector:@selector(localizedCompare:)];
req.sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *result = [self.managedObjectContext executeFetchRequest:req error:nil];
"MyEntity" is a Core Data entity with one attribute "name" of type String.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With