localizedCaseInsensitiveCompare似乎不适用于swedish字符 [英] localizedCaseInsensitiveCompare does not seem to work with swedish characters

查看:319
本文介绍了localizedCaseInsensitiveCompare似乎不适用于swedish字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按字母顺序排序数组。在瑞典字母表中,字母Å是字母表中第三个最后一个字母,所以下面的数组应该排序为 A,B,Å,而是排序为 A,Å,B 。这个行为的原因是什么?

I'm trying to sort an array alphabetically. In the swedish alphabet the letter Å is third last letter in the alphabet so the below array should be sorted like A, B, Å but instead it is sorted like A, Å, B. What could be the reason for this behaviour?

NSArray *test = @[@"Å", @"A", @"B"];

NSArray *sortedTest = [test sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
// Output is A, Å, B


推荐答案

p>也许当前的语言环境不是瑞典语语言环境?

Perhaps the current locale is not the Swedish locale?

如果您明确使用瑞典语语言环境对字符串进行排序,它会正常工作:

It works as expected if you explicitly use a Swedish locale for sorting the strings:

NSArray *test = @[@"Å", @"A", @"B"];
NSLocale *swedish = [[NSLocale alloc] initWithLocaleIdentifier:@"sv"];

NSArray *sortedTest = [test sortedArrayWithOptions:0
                                   usingComparator:^(NSString  *v1, NSString *v2) {
    return [v1 compare:v2 options:NSCaseInsensitiveSearch
                 range:NSMakeRange(0, [v1 length])
                locale:swedish];
}];

// Output: A, B, Å

这篇关于localizedCaseInsensitiveCompare似乎不适用于swedish字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆