實現(xiàn)目標(biāo):數(shù)組中文字奇颠,按照首字母排序集歇,并相同字放置一起(若首字母相同叮雳,查找第二個字母)
工具:X-code
大致思路:分兩次實現(xiàn)赦役,第一次按照字母排序麻敌,第二次判斷首位漢字
注意:本次使用的的數(shù)組,是一個模型數(shù)組掂摔,漢字是模型數(shù)組中備注昵稱(如果沒有术羔,則取模型中的名字),如有需求,請自行修改
1.常規(guī)冒泡算法排序乙漓,按字母排序
+ (NSMutableArray *)paiduWithList:(NSArray *)list{
NSMutableArray *people = [NSMutableArray arrayWithArray:list];
for (int i = 0; i < people.count; ++i) {
for (int j = 0; j < people.count-1; ++j) {
LYOnePeopleModel *model1 = people[j];
LYOnePeopleModel *model2 = people[j+1];
if ([self isBiggerwithOne:model1 andTwo:model2]) {
[people exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSMutableArray *arr = [self keywordPaixuList:people];
return arr;
}
2.上個步驟中使用的比較方法
+ (BOOL)isBiggerwithOne:(LYOnePeopleModel *)model1 andTwo:(LYOnePeopleModel *)model2{? ? ? ??
NSString *name1;? ? NSString *name2;? ??
if (model1.remarkName.length > 0 && ![model1.remarkName isEqualToString:@""] && ![model1.remarkName isEqualToString:@"(null)"]) {? ? ? ??
name1 = model1.remarkName;? ??
}else{? ? ? ?
?name1 = model1.name;? ?
?}? ??
if (model2.remarkName.length > 0 && ![model2.remarkName isEqualToString:@""] && ![model2.remarkName isEqualToString:@"(null)"]) {
name2 = model2.remarkName;
}else{
name2 = model2.name;
}
//將取出的名字轉(zhuǎn)換成字母
NSMutableString *pinyin1 = [name1 mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformStripCombiningMarks, NO);
//將拼音轉(zhuǎn)換成大寫拼音
NSString * upPinyin1 = [pinyin1 uppercaseString];
//取出第一個首字母當(dāng)做字典的key
NSString * firstStr1 = [upPinyin1 substringToIndex:1];
NSMutableString *pinyin2 = [name2 mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformStripCombiningMarks, NO);
//將拼音轉(zhuǎn)換成大寫拼音
NSString * upPinyin2 = [pinyin2 uppercaseString];
//取出第一個首字母當(dāng)做字典的key
NSString * firstStr2 = [upPinyin2 substringToIndex:1];
if (firstStr1 > firstStr2) {
return YES;
}else if (firstStr1 < firstStr2){
return NO;
}else{
//如果相等比較第二位
if (upPinyin1.length > 1) {
if (upPinyin2.length > 1) {
//判斷第二位
NSString *secStr1 = [upPinyin1 substringWithRange:NSMakeRange(1, 1)];
NSString *secStr2 = [upPinyin2 substringWithRange:NSMakeRange(1, 1)];
if (secStr1 > secStr2) {
return YES;
}else if (secStr1 < secStr2){
return NO;
}else{
if (upPinyin1.length > 2) {
//判斷第三位
if (upPinyin2.length > 2) {
NSString *treStr1 = [upPinyin1 substringWithRange:NSMakeRange(2, 1)];
NSString *treStr2 = [upPinyin2 substringWithRange:NSMakeRange(2, 1)];
if (treStr1 > treStr2) {
return YES;
}else if (treStr1 < treStr2){
return NO;
}else{
if (upPinyin1.length > 3) {
//判斷第四位
NSString *fourStr1 = [upPinyin1 substringWithRange:NSMakeRange(3, 1)];
NSString *fourStr2 = [upPinyin2 substringWithRange:NSMakeRange(3, 1)];
if (fourStr1 > fourStr2) {
return YES;
}else{
return NO;
}
}else{
return NO;
}
}
}else{
return YES;
}
}else{
return NO;
}
}
}else{
return YES;
}
}else{
return NO;
}
}
}
3.按照漢字第一位相同的级历,放置一起
+ (NSMutableArray *)keywordPaixuList:(NSArray *)list{? ??
NSMutableArray *people = [NSMutableArray arrayWithArray:list];? ??
int num = 0;? ?
?for (int i = 0; i < people.count; ++i) {? ? ? ? ? ? ? ?
int creat = 1;? ? ? ??
for (int j = num+creat; j < people.count; j++) {? ? ? ? ? ??
LYOnePeopleModel *model1 = people[i];? ? ? ? ? ??
LYOnePeopleModel *model2 = people[j];? ? ? ? ? ??
NSString *name1;? ? ? ? ? ? NSString *name2;? ? ? ? ? ??
if (model1.remarkName.length > 0 && ![model1.remarkName isEqualToString:@""] && ![model1.remarkName isEqualToString:@"(null)"]) {? ? ? ? ? ? ? ??
name1 = model1.remarkName;? ? ? ? ? ??
}else{? ? ? ? ? ? ? ??
name1 = model1.name;? ? ? ? ? ??
}? ? ? ? ? ??
if (model2.remarkName.length > 0 && ![model2.remarkName isEqualToString:@""] && ![model2.remarkName isEqualToString:@"(null)"]) {
name2 = model2.remarkName;
}else{
name2 = model2.name;
}
if ([[name1 substringToIndex:1] isEqualToString:[name2 substringToIndex:1]]) {
[people exchangeObjectAtIndex:num+creat withObjectAtIndex:j];
creat++;
}
}
num = num+creat;
i = num-1;
}
return people;
}
這樣基本就可以實現(xiàn),原諒樓主不會排布簡書頁面叭披,抱歉