所以我有一个应用程序,我想在其中按任意区域设置对数据进行排序 - 而不是手机区域设置。 在我看来,iOS 为中文提供的排序顺序是错误的(但我不懂中文)所以有人可以告诉我这是否正确吗?
我相信对于简体/繁体中文,数字1到10是:
一、二、三、四、五、六、七、八、九、十
这是我的测试代码:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"zh_Hans_CN"];
NSArray *outOfOrder = @[@"十", @"七", @"一", @"二", @"四", @"三", @"五", @"六", @"八", @"九"];
NSSortDescriptor *nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2)
{
return [(NSString *)obj1 compare:(NSString *)obj2 options:NSCaseInsensitiveSearch range:NSMakeRange(0, [(NSString *)obj1 length]) locale:locale];
}];
NSArray *sorted = [outOfOrder sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameDescriptor]];
但是排序后的数组是这样的: 八, 二、 九, 六、 七、 三、 十, 四、 五、 一
当然中文应该像其他语言一样排序,相当于 1..9 后跟“单词”字符等?
最佳答案
您可以使用 NSLocale 对中文进行排序。 iOS 支持 ICU alphabetic .
汉字整理支持:
- 拼音:A B C D ...
- 行程:1 2 3 ...
- Radical-Stroke:部首
这里有一些示例代码供您引用:
NSLocale* strokeSortingLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh@collation=stroke"];
NSComparator compareByStroke = ^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:0 range:NSMakeRange(0, [obj1 length]) locale:strokeSortingLocale];
};
NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name_ch" ascending:YES comparator:compareByStroke];
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSArray* sortedArray = [self.result sortedArrayUsingDescriptors:sortDescriptors];
关于ios - 使用 NSLocale zh_Hans_CN 在 iOS 上的中文数字排序顺序不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25685734/