开发者

How can accented strings be sorted in Objective-c in a way that the accented always follows the base character?

I try to sort a list of strings alphabetically in Objective-C. Since the list is hungarian, it contains accented characters (they are part of the "official" hungarian alphabet), and they should be ordered as: a á b c d e é ...

The problem is that iOS orders that list as the accents wouldn't exist, so these three lines are sorted as: abc ábc acc But sould be sorted as: abc acc ábc

I tried to sort the string like this:

static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;

NSRange string1Range = NSMakeRange(0, 4);

NSCo开发者_开发百科mparisonResult res = [@"Abel" compare:@"Áael" options:comparisonOptions range:string1Range locale:[[NSLocale alloc] initWithLocaleIdentifier:@"hu_HU"]];
switch (res) {
    case NSOrderedAscending:
        NSLog(@"Ascending");
        break;

    case NSOrderedDescending:
        NSLog(@"Descending");
        break;

    default:
        NSLog(@"Same");
        break;
}

The result is descending, so it wants to swap the two to be in order, but it shouldn't. There is a NSComparisionOption element called NSDiacriticInsensitiveSearch which means - as the documentation states - that the comparison should ignore the accents and compare as if it were the base character. Unfortunately, the comparison seems to work that way even without defining this option.

I use iOS version 4.3 for iPhone.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜