开发者

iPhone CoreData: Sort with umlauts

开发者 https://www.devze.com 2023-04-08 03:26 出处:网络
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

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 = [开发者_StackOverflow中文版[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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号