简体   繁体   中英

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.

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:)]

You should use UILocalizedIndexedCollation for doing sorting and categorizing entries into sections. The code for implementing this is in the question NSFetchedResultsController vs 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM