IOS implementation of contacts sorted by initials

Time:2020-11-13

The requirements of the contact function are generally sorted according to the first letter, and the same surname must be continuous, and other surnames can not be interspersed. Baidu saw uilocalized indexed collation provides us with a very convenient sorting method. It does not need to convert Chinese into pinyin. However, there is a disadvantage that if the surname has polyphonic characters, it can not be distinguished (for example It will be added to the (Ceng) group

The following code:

1. Create a class to manage linkmansort

. m file

NSString *const CYPinyinGroupResultArray = @”CYPinyinGroupResultArray”;

NSString *const CYPinyinGroupCharArray = @”CYPinyinGroupCharArray”;

@implementation LinkManSort

//Sort arrays by initials + (nsdictionary) sortobjectsaccordingtoinitialwith: (nsarray) willsortarray sortkey: (nsstring *) sortkey{

//Initialize uilocalizedindexedcollation
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
//The number of collation indexes is obtained. Here are 27 (26 letters and 1 ා)
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
//Initialize an array newsectionsarray to store the final data. The data model we want to get should be @ [@ [data array starting with a], @ [data array starting with b], @ [data array starting with C],... @ [data group starting with #]
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//Initialize 27 empty arrays and add them to 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];
//Put each name under a section
for (id Model in willSortArr) {
 //Get the location of the value of the name attribute, such as "lindane". The initial letter is L. it ranks 11th in a ~ Z (the first digit is 0), and the sectionnumber is 11
 NSInteger sectionNumber = [collation sectionForObject:Model collationStringSelector:NSSelectorFromString(sortkey)];
 //Add the p with the name "lindane" to the 11th array in the new sections array
 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);
//The arrays in each section are sorted by the name attribute
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
 NSMutableArray *personArrayForSection = newSectionsArray[index];
 NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
 newSectionsArray[index] = sortedPersonArrayForSection;
}
//Delete empty array
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{

//Array de duplication
NSMutableArray *noRepeat = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableSet *set = [[NSMutableSet alloc]initWithArray:firstChararry];
[set enumerateObjectsUsingBlock:^(id obj , BOOL *stop){

 [noRepeat addObject:obj];
}];
//Alphabetic sorting
NSArray *resultkArrSort1 = [noRepeat sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
 return [obj1 compare:obj2 options:NSNumericSearch];
}];
//Put "ා" in the last place
NSMutableArray *resultkArrSort2 = [[NSMutableArray alloc]initWithArray:resultkArrSort1];
if ([resultkArrSort2 containsObject:@"#"]) {
 [resultkArrSort2 removeObject:@"#"];
 [resultkArrSort2 addObject:@"#"];
}

return resultkArrSort2;

}

. h file

First introduce the framework UIKit / UIKit. H / * * to get the model array * / UIKit_ EXTERN NSString *const CYPinyinGroupResultArray;

/*** get the array of wrapped letter * / UIKit_ EXTERN NSString *const CYPinyinGroupCharArray; @interface LinkManSort : NSObject +(NSDictionary )sortObjectsAccordingToInitialWith:(NSArray )willSortArr SortKey:(NSString *)sortkey ;

Call in VC

Array * array * array * array * array = @ [@ @ @ “name”: @ “Li Li”}, @ {@ “name”: @ “Li’an”}, @ {@ “name”: @ “Liuxing”}, @ @ @ “name”: @ “liuxiaomi”}, @ @ @ “name”: @ “Suyin”}, @ @ @ “name”: @ “weijiajia”}, @ {@ “name”: @ “Li Hua”}, @ @ @ “name”: @ “Yang Bo”}, @ @ @ “name”: @ “Chen Heng”}, @ @ @ @ @ @ @ “name”: @ “yellow babya Ya” @ @ @ “yellow babya” @ @ @ @ “yellow babya Ya” @ @ “yellow babya” @ @ “yellow babya” yellow babya ya ya”}, @ {@” name “: @” Qiu Po “}, @{@ “name”: @ “Li Ke”}, @ {@ “name”: @ “123456”}, @ {@ “name”: @ “Wei Lilin”}, @ {@ “name”: @ “Chen Yao”}];


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 * resultar1 = DCIT [cypinygroupresultarray]; // sorted personmodel array
Nsarray * resultar2 = DCIT [cypinygroupchararray]; // sorted initial array

It’s done!

The above example of sorting contacts by initials in IOS is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]