iOS数组里面元素按字母排序


//排序方法
        [self.groupUserArray sortUsingComparator:^NSComparisonResult(GroupUserModel *obj1, GroupUserModel *obj2) {
            NSMutableString *str1 = [obj1.name mutableCopy];
            NSMutableString *str2 = [obj2.name mutableCopy];
            str1 = [self getLatinWithFullName:str1];
            str2 = [self getLatinWithFullName:str2];
            NSComparisonResult result = [str1 compare:str2];
            return result;
        }];
 1 //优化速度
 2 - (NSString *)getLatinWithFullName:(NSString *)name{
 3     //1.先从缓存中获得latin,然后再判断是否存在,再将非字符串转化成拉丁字母
 4     NSString* latin = [self.pinyinDic objectForKey:name];
 5     if (latin) {
 6         return latin;
 7     }else {
 8         latin = [NSString stringWithString:name];
 9         CFStringTransform((__bridge CFMutableStringRef)latin, NULL, kCFStringTransformMandarinLatin, NO);
10         CFStringTransform((__bridge CFMutableStringRef)latin, NULL, kCFStringTransformStripDiacritics, NO);
11         [self.pinyinDic setObject:latin forKey:name];
12         return latin;
13 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM