NSInteger sortObjects(id obj1, id obj2,void *context)
{
NSMutableString * str1 = [[NSMutableString alloc]initWithString:obj1];
if (CFStringTransform((__bridge CFMutableStringRef)str1, 0, kCFStringTransformMandarinLatin, NO)) {
} NSMutableString * str2 = [[NSMutableString alloc]initWithString:obj2];
if (CFStringTransform((__bridge CFMutableStringRef)str2, 0, kCFStringTransformMandarinLatin, NO)) {
}
return [str1 localizedCompare:str2];
}
//先转为拼音,然后再进行排序。上面是进行转拼音,然后再排序的过程。 [otherNameArr sortUsingFunction:sortObjects context:NULL];//数组进行调用。 然后就完了。得到的数组就是从A~Z的过程了。
例子:
- (void)viewDidLoad {
[super viewDidLoad]; NSMutableArray * arr = [NSMutableArray arrayWithObjects:@"中文",@"linxl",@"唐唐唐",@"abc", nil];
[arr sortUsingFunction:sortObjects context:NULL];
NSLog(@"arr - %@",arr);
} NSInteger sortObjects(id obj1, id obj2,void *context)
{
NSMutableString * str1 = [[NSMutableString alloc]initWithString:obj1];
if (CFStringTransform((__bridge CFMutableStringRef)str1, 0, kCFStringTransformMandarinLatin, NO)) {
} NSMutableString * str2 = [[NSMutableString alloc]initWithString:obj2];
if (CFStringTransform((__bridge CFMutableStringRef)str2, 0, kCFStringTransformMandarinLatin, NO)) {
}
return [str1 localizedCompare:str2];
}
打印:
2016-04-26 16:51:15.350 dd[65248:767376] arr - (
abc,
linxl,
"\U5510\U5510\U5510",
"\U4e2d\U6587"