Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone CoreData: Sort with umlauts

I have an app that fetches data (a list of countries) from an sqlite database. The countries are in German and contain umlauts, for example Österreich. I want to fetch those countries sorted by name and would expect Österreich to be near other countries with "O", but it is at the end of the results instead.

This code is used to fetch the sorted countries:

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

 [fetchRequest setEntity: [NSEntityDescription entityForName: @"Continent" inManagedObjectContext: moc]];
    [fetchRequest setSortDescriptors: [NSArray arrayWithObject: [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES] autorelease]]];

 NSError *error = nil;
 NSArray *result = [moc executeFetchRequest: fetchRequest error: &error];

Is there any way to have those special char names appear at the position I described above? Didn't find anything on the internet on this ...

Thanks a lot.

like image 632
Cornelius Avatar asked Sep 28 '11 14:09

Cornelius


2 Answers

Thanks for your answers, I could solve the problem another (easy) way!

I just had to modify the NSSortDescriptor like this:

[NSSortDescriptor sortDescriptorWithKey: @"name" ascending: YES selector: @selector(localizedCompare:)]
like image 69
Cornelius Avatar answered Nov 08 '22 09:11

Cornelius


You should use UILocalizedIndexedCollation for doing sorting and categorizing entries into sections. The code for implementing this is in the question NSFetchedResultsController v.s. UILocalizedIndexedCollation

The UILocalizedIndexedCollation was built to be able to on a per-language basis categorize words based on the current language settings. Á and à will be put in section A.

like image 3
Brent Priddy Avatar answered Nov 08 '22 11:11

Brent Priddy