2013-01-05 53 views
2

我想按字母順序排列數組。在瑞典語字母表中,字母Å是字母表中的倒數第三個字母,因此下面的數組應按A, B, Å排序,但它會按A, Å, B排序。什麼可能是這種行爲的原因?localizedCaseInsensitiveCompare似乎不適用於瑞典語字符

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

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

是localizedCaseInsensitiveCompare:在文檔中?我沒有找到它。 –

+1

@RamyAlZuhouri:[localizedCaseInsensitiveCompare:](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/ NSString/localizedCaseInsensitiveCompare :) –

回答

5

也許當前的語言環境不是瑞典的語言環境?

它按預期工作,如果你明確地使用瑞典區域排序的字符串:

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, Å 
+0

啊,當然可能是這樣的。稍後嘗試一下。謝謝。 –