1、获取单例的collation
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
//标题数组
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
//设置sections数组初始化:元素包含userObjs数据的空数据
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
2、分类 - 将用户数据进行分类,存储到对应的sesion数组中
for (MTDUserBaseModel *p in self.friendListRequest.friendMutableArray) {
NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(userNickname)];
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:p];
}
3、排序 - 对每个已经分类的数组中的数据进行排序
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *personArrayForSection = newSectionsArray[index];
NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(userNickname)];
newSectionsArray[index] = sortedPersonArrayForSection;
}