用ÅÄÖ排序数组

时间:2012-11-02 22:27:36

标签: iphone objective-c xcode cocoa-touch

我对iPhone / OSX开发完全不熟悉,我正在尝试按字母顺序对填充了对象的NSMutableArray进行排序,但是我失败了。以Å或Ä开头的项目最后是以A开头的项目,以Ö开头的项目最后是以O开头的项目。我尝试过在Stackoverflow上找到的不同的排序方法,但它们似乎都没有用(对我而言)至少)

    NSSortDescriptor* stationDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    NSArray* descriptors = [NSArray arrayWithObject:stationDescriptor];
    [stations sortUsingDescriptors:descriptors];


    [stations sortUsingComparator:^NSComparisonResult(Station* obj1, Station* obj2){
        return [[obj1 name] localizedCaseInsensitiveCompare:[obj2 name]];
    }];

好的,进步!通过将模拟器上的语言更改为瑞典语,我成功地实现了这一功能,当手机设置为英语时,有什么方法可以得到相同的结果吗?我知道在C#中,您可以在排序/比较字符串时发送对特定文化的引用。

/尤

1 个答案:

答案 0 :(得分:3)

查看NSString的文档可以给出答案。

您正在使用localizedCaseInsensitiveCompare:以及此州的文档

  blah blah blah

     

另见
   - 比较:选项:范围:区域设置:

所以跳到compare:options:range:locale:的文档,你可以弄明白。你需要像

这样的东西
return [[obj1 name] compare:[obj2 name]
                    options:NSCaseInsensitiveSearch
                      range:NSMakeRange(0, [obj1 length])
                     locale:whateverLocaleYouWant];
相关问题