当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS实现联系人按照首字母进行排序的实例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

联系人功能的需求一般都会有按照首字母排序,并且会要求同一个姓的就要连续起来中间不能穿插别的姓,百度了一下看到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实现联系人按照首字母进行排序的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网