所以我有一个应用程序,我想通过任意的地区来分类数据,而不是手机区域。在我看来,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,后面是‘单词’字符等等。
发布于 2016-05-17 14:36:43
您可以使用NSLocale对中文进行排序。iOS支持ICU字母。
汉代校对支持:
以下是一些供您参考的示例代码:
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];https://stackoverflow.com/questions/25685734
复制相似问题