1. 程式人生 > >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];//排好順序的首字母陣列

完成