美文网首页iOS开发iOS DeveloperiOS开发
iOS数组按中文关键字以字母序排序

iOS数组按中文关键字以字母序排序

作者: 木子de李 | 来源:发表于2017-08-10 15:54 被阅读71次

    实现目标:数组中文字,按照首字母排序,并相同字放置一起(若首字母相同,查找第二个字母)

    工具:X-code

    大致思路:分两次实现,第一次按照字母排序,第二次判断首位汉字

    注意:本次使用的的数组,是一个模型数组,汉字是模型数组中备注昵称(如果没有,则取模型中的名字),如有需求,请自行修改

    1.常规冒泡算法排序,按字母排序

    + (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;

    }

    //将取出的名字转换成字母

    NSMutableString *pinyin1 = [name1 mutableCopy];

    CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformMandarinLatin, NO);

    CFStringTransform((__bridge CFMutableStringRef)pinyin1, NULL, kCFStringTransformStripCombiningMarks, NO);

    //将拼音转换成大写拼音

    NSString * upPinyin1 = [pinyin1 uppercaseString];

    //取出第一个首字母当做字典的key

    NSString * firstStr1 = [upPinyin1 substringToIndex:1];

    NSMutableString *pinyin2 = [name2 mutableCopy];

    CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformMandarinLatin, NO);

    CFStringTransform((__bridge CFMutableStringRef)pinyin2, NULL, kCFStringTransformStripCombiningMarks, NO);

    //将拼音转换成大写拼音

    NSString * upPinyin2 = [pinyin2 uppercaseString];

    //取出第一个首字母当做字典的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;

    }

    这样基本就可以实现,原谅楼主不会排布简书页面,抱歉

    相关文章

      网友评论

      本文标题:iOS数组按中文关键字以字母序排序

      本文链接:https://www.haomeiwen.com/subject/sbmhrxtx.html