中文拼音简单排序

117 篇文章 0 订阅
  1. // 数字和字母  
  2.     class sortNumAndChar implements Comparator {  
  3.   
  4.         public int compare(Object o1, Object o2) {  
  5.             // TODO Auto-generated method stub  
  6.             String ostr1 = "";  
  7.             String ostr2 = "";  
  8.             return ostr1.compareToIgnoreCase(ostr2);  
  9.         }  
  10.   
  11.     }  
  12.   
  13.     // 中文按拼音排序  
  14.     class sortCN implements Comparator {  
  15.   
  16.         public int compare(Object o1, Object o2) {  
  17.             // TODO Auto-generated method stub  
  18.             String ostr1 = "";  
  19.             String ostr2 = "";  
  20.   
  21.             for (int i = 0; i < ostr1.length() && i < ostr2.length(); i++) {  
  22.   
  23.                 int codePoint1 = ostr1.charAt(i);  
  24.                 int codePoint2 = ostr2.charAt(i);  
  25.                 if (Character.isSupplementaryCodePoint(codePoint1)  
  26.                         || Character.isSupplementaryCodePoint(codePoint2)) {  
  27.                     i++;  
  28.                 }  
  29.                 if (codePoint1 != codePoint2) {  
  30.                     if (Character.isSupplementaryCodePoint(codePoint1)  
  31.                             || Character.isSupplementaryCodePoint(codePoint2)) {  
  32.                         return codePoint1 - codePoint2;  
  33.                     }  
  34.                     String pinyin1 = pinyin((char) codePoint1);  
  35.                     String pinyin2 = pinyin((char) codePoint2);  
  36.   
  37.                     if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字  
  38.                         if (!pinyin1.equals(pinyin2)) {  
  39.                             return pinyin1.compareTo(pinyin2);  
  40.                         }  
  41.                     } else {  
  42.                         return codePoint1 - codePoint2;  
  43.                     }  
  44.                 }  
  45.             }  
  46.             return ostr1.length() - ostr2.length();  
  47.         }  
  48.   
  49.         private String pinyin(char c) {  
  50.             String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);  
  51.             if (pinyins == null) {  
  52.                 return null;  
  53.             }  
  54.             return pinyins[0];  
  55.         }  
  56.   
  57.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值