欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS实现联系人按照首字母进行排序的实例,ios联系人

来源: 开发者 投稿于  被查看 27419 次 评论:8

iOS实现联系人按照首字母进行排序的实例,ios联系人


联系人功能的需求一般都会有按照首字母排序,并且会要求同一个姓的就要连续起来中间不能穿插别的姓,百度了一下看到UILocalizedIndexedCollation给我们提供了很方便的排序方法,它不需要将中文转为拼音,但是有一个缺点就是如果姓氏存在多音字就无法区分(例如:姓增,它会被分配到C (ceng)组)

下面贴代码:

1,建一个类进行管理LinkManSort

.m文件

NSString *const CYPinyinGroupResultArray = @”CYPinyinGroupResultArray”;

NSString *const CYPinyinGroupCharArray = @”CYPinyinGroupCharArray”;

@implementation LinkManSort

// 按首字母分组排序数组 +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey {

// 初始化UILocalizedIndexedCollation
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
//得出collation索引的数量,这里是27个(26个字母和1个#)
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
//初始化一个数组newSectionsArray用来存放最终的数据,我们最终要得到的数据模型应该形如@[@[以A开头的数据数组], @[以B开头的数据数组], @[以C开头的数据数组], ... @[以#(其它)开头的数据数组]]
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//初始化27个空数组加入newSectionsArray
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
 NSMutableArray *array = [[NSMutableArray alloc] init];
 [newSectionsArray addObject:array];
}
NSLog(@"newSectionsArray %@ %@",newSectionsArray,collation.sectionTitles);
NSMutableArray *firstChar = [NSMutableArray arrayWithCapacity:10];
//将每个名字分到某个section下
for (id Model in willSortArr) {
 //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
 NSInteger sectionNumber = [collation sectionForObject:Model collationStringSelector:NSSelectorFromString(sortkey)];
 //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
 NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
 [sectionNames addObject:Model];
 NSString * str= collation.sectionTitles[sectionNumber];
 [firstChar addObject:str];
 NSLog(@"sectionNumbersectionNumber %ld %@",sectionNumber,str);
}
NSArray *firstCharResult = [self SortFirstChar:firstChar];

NSLog(@"firstCharResult== %@",firstCharResult);
//对每个section中的数组按照name属性排序
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
 NSMutableArray *personArrayForSection = newSectionsArray[index];
 NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
 newSectionsArray[index] = sortedPersonArrayForSection;
}
//删除空的数组
NSMutableArray *finalArr = [NSMutableArray new];
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
 if (((NSMutableArray *)(newSectionsArray[index])).count != 0) {
  [finalArr addObject:newSectionsArray[index]];
 }
}
return @{CYPinyinGroupResultArray:finalArr,
   CYPinyinGroupCharArray:firstCharResult};

}

+(NSArray )SortFirstChar:(NSArray )firstChararry{

//数组去重复
NSMutableArray *noRepeat = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableSet *set = [[NSMutableSet alloc]initWithArray:firstChararry];
[set enumerateObjectsUsingBlock:^(id obj , BOOL *stop){

 [noRepeat addObject:obj];
}];
//字母排序
NSArray *resultkArrSort1 = [noRepeat sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
 return [obj1 compare:obj2 options:NSNumericSearch];
}];
//把”#“放在最后一位
NSMutableArray *resultkArrSort2 = [[NSMutableArray alloc]initWithArray:resultkArrSort1];
if ([resultkArrSort2 containsObject:@"#"]) {
 [resultkArrSort2 removeObject:@"#"];
 [resultkArrSort2 addObject:@"#"];
}

return resultkArrSort2;

}

.h文件

先引入框架UIKit/UIKit.h /** * 获取model数组 */ UIKIT_EXTERN NSString *const CYPinyinGroupResultArray;

/** * 获取所包函字母的数组 */ UIKIT_EXTERN NSString *const CYPinyinGroupCharArray; @interface LinkManSort : NSObject +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey ;

在VC里面调用

NSArray *arr = @[@{@”name”:@”李立”},@{@”name”:@” 李安”},@{@”name”:@”刘星”},@{@”name”:@”刘小米”},@{@”name”:@”苏音”},@{@”name”:@”韦佳佳”},@{@”name”:@”李华”},@{@”name”:@”杨波”},@{@”name”:@”陈恒”},@{@”name”:@”黄呀呀”},@{@”name”:@”邱珀”},@{@”name”:@”李克”},@{@”name”:@”123456”},@{@”name”:@”韦立林”},@{@”name”:@”陈瑶”}];

NSMutableArray *marr = [NSMutableArray arrayWithCapacity:10];
for (NSDictionary *dict in arr) {
 PersonModel *model =[[PersonModel alloc]init];// dict[@"name"];
 model.name =dict[@"name"];
 [marr addObject:model];

}

NSDictionary *dcit= [LinkManSort sortObjectsAccordingToInitialWith:marr SortKey:@”name”];

NSArray *resultarr1 = dcit[CYPinyinGroupResultArray];//排好顺序的PersonModel数组
NSArray *resultarr2 = dcit[CYPinyinGroupCharArray];//排好顺序的首字母数组

完成啦!

以上这篇iOS实现联系人按照首字母进行排序的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

用户评论