根據(jù)數(shù)據(jù)的拼音或者文案的首字母排序
方法如下:
static NSString * const CYPinyinGroupResultArray = @"CYPinyinGroupResultArray";
static NSString * const CYPinyinGroupCharArray = @"CYPinyinGroupCharArray";
// 按首字母分組排序數(shù)組
- (NSDictionary *)sortObjectsAccordingToInitialWith:(NSArray *)willSortArr SortKey:(NSString *)sortkey {
// 初始化UILocalizedIndexedCollation
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
// 得出collation索引的數(shù)量剃执,這里是27個(gè)(26個(gè)字母和1個(gè)#)
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
// 初始化一個(gè)數(shù)組newSectionsArray用來(lái)存放最終的數(shù)據(jù)懈息,我們最終要得到的數(shù)據(jù)模型應(yīng)該形如
// @[@[以A開(kāi)頭的數(shù)據(jù)數(shù)組], @[以B開(kāi)頭的數(shù)據(jù)數(shù)組], @[以C開(kāi)頭的數(shù)據(jù)數(shù)組], ... @[以#(其它)開(kāi)頭的數(shù)據(jù)數(shù)組]]
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//初始化27個(gè)空數(shù)組加入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];
//將每個(gè)名字分到某個(gè)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個(gè)數(shù)組中去
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:Model];
//拿出每名字的首字母
NSString * str= collation.sectionTitles[sectionNumber];
[firstChar addObject:str];
NSLog(@"sectionNumbersectionNumber %ld %@",sectionNumber,str);
}
//返回首字母排好序的數(shù)據(jù)
NSArray *firstCharResult = [self SortFirstChar:firstChar];
NSLog(@"firstCharResult== %@",firstCharResult);
//對(duì)每個(gè)section中的數(shù)組按照name屬性排序
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *personArrayForSection = newSectionsArray[index];
NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
newSectionsArray[index] = sortedPersonArrayForSection;
}
//刪除空的數(shù)組
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
{
//數(shù)組去重復(fù)
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;
}
方法調(diào)用:
因?yàn)?collationStringSelector方法需要 傳入一個(gè) SEL遣耍,所以需要把數(shù)據(jù)轉(zhuǎn)成Model
@interface BSModel : NSObject
@property (nonatomic, copy) NSString *name;
@end
然后調(diào)用方法
NSArray *models = [NSArray yy_modelArrayWithClass:BSModel.class json:[responseObject objectForKey:@"data"]];
NSDictionary *dict = [self sortObjectsAccordingToInitialWith:models SortKey:@"name"];
self.sectionDataSource = [dict objectForKey:CYPinyinGroupCharArray];
self.cellDataSource = [dict objectForKey:CYPinyinGroupResultArray];
數(shù)據(jù)如下
[
{
"name" : "順豐速運(yùn)"
},
{
"name" : "百世快遞"
},
{
"name" : "中通快遞"
},
{
"name" : "申通快遞"
},
{
"name" : "圓通速遞"
},
{
"name" : "韻達(dá)速遞"
},
{
"name" : "郵政快遞包裹"
},
{
"name" : "EMS"
},
{
"name" : "天天快遞"
},
{
"name" : "京東快遞"
},
{
"name" : "優(yōu)速快遞"
},
{
"name" : "德邦快遞"
},
{
"name" : "宅急送"
},
{
"name" : "TNT快遞"
},
{
"name" : "UPS"
},
{
"name" : "DHL"
},
{
"name" : "FEDEX聯(lián)邦(國(guó)內(nèi)件)"
},
{
"name" : "FEDEX聯(lián)邦(國(guó)際件)"
}
]