2012-11-02 85 views
0

我完全不熟悉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#中,您可以在排序/比较字符串时发送对特定文化的引用。

/维克托

回答

3

望着文档为NSString给你答案。

您正在使用localizedCaseInsensitiveCompare:并且这种状态的文档

等等等等

请参见
- 比较:选项:范围:区域:

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

return [[obj1 name] compare:[obj2 name] 
        options:NSCaseInsensitiveSearch 
         range:NSMakeRange(0, [obj1 length]) 
        locale:whateverLocaleYouWant]; 
+0

感谢您的帮助! – Viktor

+0

不应该是NSMakeRange(0,[[obj1 name] length])? – oskob

+0

正是我在找什么,我加了NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@“nb_NO”]; 它开始为我工作。 – york