按照拼音对数组中的中文字符串排序的算法

按照拼音对数组中的中文字符串排序的算法,不是很难,这里直接放上代码,Demo去最下面找:
注:里面用到一个叫George的牛人写的中文转拼音首字母的函数pinyinFirstLetter,因为内容实在是太多了就不贴上来了。
里面还用到一个叫ChineseString的类,里面只有两个NSString对象(string和pinYin)用于存放原字符串和字符串中每个字的拼音首字母
复制代码
  1.         //结果看输出
  2.         //Step1:初始化
  3.         NSMutableArray *stringsToSort=[NSArray arrayWithObjects:
  4.                                 @"电脑",
  5.                                 @"显示器",
  6.                                 @"你好",
  7.                                 @"推特",
  8.                                 @"乔布斯",
  9.                                 @"再见",
  10.                                 @"暑假作业",
  11.                                 @"键盘",
  12.                                 @"鼠标",
  13.                                 @"谷歌",
  14.                                 @"苹果",
  15.                                 nil];
  16.         
  17.         //Step1输出
  18.         NSLog(@"尚未排序的NSString数组:");
  19.         for(int i=0;i<[stringsToSort count];i++){
  20.             NSLog(@"%@",[stringsToSort objectAtIndex:i]);
  21.         }
  22.         
  23.         
  24.         //Step2:获取字符串中文字的拼音首字母并与字符串共同存放
  25.         NSMutableArray *chineseStringsArray=[NSMutableArray array];
  26.         for(int i=0;i<[stringsToSort count];i++){
  27.             ChineseString *chineseString=[[ChineseString alloc]init];
  28.             
  29.             chineseString.string=[NSString stringWithString:[stringsToSort objectAtIndex:i]];
  30.             
  31.             if(chineseString.string==nil){
  32.                 chineseString.string=@"";
  33.             }
  34.             
  35.             if(![chineseString.string isEqualToString:@""]){
  36.                 NSString *pinYinResult=[NSString string];
  37.                 for(int j=0;j
  38.                     NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
  39.                     
  40.                     pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
  41.                 }
  42.                 chineseString.pinYin=pinYinResult;
  43.             }else{
  44.                 chineseString.pinYin=@"";
  45.             }
  46.             [chineseStringsArray addObject:chineseString];
  47.         }
  48.         
  49.         //Step2输出
  50.         NSLog(@"\n\n\n转换为拼音首字母后的NSString数组");
  51.         for(int i=0;i<[chineseStringsArray count];i++){
  52.             ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
  53.             NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
  54.         }
  55.         
  56.         
  57.         
  58.         //Step3:按照拼音首字母对这些Strings进行排序
  59.         NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
  60.         [chineseStringsArray sortUsingDescriptors:sortDescriptors];
  61.         
  62.         //Step3输出
  63.         NSLog(@"\n\n\n按照拼音首字母后的NSString数组");
  64.         for(int i=0;i<[chineseStringsArray count];i++){
  65.             ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
  66.             NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
  67.         }
  68.         
  69.         // Step4:如果有需要,再把排序好的内容从ChineseString类中提取出来
  70.         NSMutableArray *result=[NSMutableArray array];
  71.         for(int i=0;i<[chineseStringsArray count];i++){
  72.             [result addObject:((ChineseString*)[chineseStringsArray objectAtIndex:i]).string];
  73.         }
  74.         
  75.         //Step4输出
  76.         NSLog(@"\n\n\n最终结果:");
  77.         for(int i=0;i<[result count];i++){
  78.             NSLog(@"%@",[result objectAtIndex:i]);
  79.         }
  80.         
  81.         //程序结束
  82.         
  83.         NSLog(@"\n\n\nDemo By Hxy060799");


输出结果这里也贴上了:

按照拼音对数组中的中文字符串排序的算法_第1张图片


Demo:  ChineseSort.zip (78 K) 下载次数:1268 

你可能感兴趣的:(Xcode,Objective-C)