在 Objective-c 中如何对带重音的字符串进行排序,使重音始终跟随在基本字符之后?

发布于 2024-11-14 17:29:22 字数 990 浏览 2 评论 0原文

我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文