在 Objective-c 中如何对带重音的字符串进行排序,使重音始终跟随在基本字符之后?
我尝试在 Objective-C 中按字母顺序对字符串列表进行排序。由于该列表是匈牙利语,因此它包含重音字符(它们是“官方”匈牙利字母表的一部分),并且它们的顺序应为:a á bcde é ...
问题是 iOS 将列表中的重音字符排序为“ t 存在,因此这三行排序为: ABC ABC ACC 但应排序为: ABC ACC ábc
我尝试像这样对字符串进行排序:
static NSStringCompareOptions comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange(0, 4);
NSComparisonResult 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;
}
结果是降序的,因此它想交换两者以按顺序排列,但它不应该。 有一个名为 NSDiacriticInsensitiveSearch 的 NSComparisionOption 元素,这意味着 - 正如文档所述 - 比较应该忽略重音符号并像基本字符一样进行比较。不幸的是,即使没有定义此选项,比较似乎也是如此。
我使用 iPhone 的 iOS 版本 4.3。
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);
NSComparisonResult 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论