localizedCaseInsensitiveCompare does not seem to work with swedish characters

后端 未结 1 1680
时光说笑
时光说笑 2021-01-21 11:40

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

1条回答
  •  执笔经年
    2021-01-21 11:42

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

    0 讨论(0)
提交回复
热议问题