1. 程式人生 > >中英文混合排序

中英文混合排序

倫理片http://www.dotdy.com/

Java程式碼  收藏程式碼
  1. import net.sourceforge.pinyin4j.PinyinHelper;  
  2. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  7. public class HanYuUtil {  
  8.     /** 
  9.      * 將單個字元(包括單個漢字或者單個英文字母)轉換為小寫字母 
  10.      * @param c 
  11.      * @return 
  12.      */  
  13.     public String getCharacterPinYin(char c)  
  14.     {  
  15.         String[] pinyin=null;  
  16.         HanyuPinyinOutputFormat format =new
     HanyuPinyinOutputFormat();  
  17.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  18.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//漢字沒有聲調  
  19.         format.setVCharType(HanyuPinyinVCharType.WITH_V);  
  20.         try{  
  21.             pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);  
  22.         }catch(BadHanyuPinyinOutputFormatCombination e){  
  23.             e.printStackTrace();  
  24.         }  
  25.         // 如果c不是漢字,toHanyuPinyinStringArray會返回null  
  26.         if (pinyin == null)  
  27.             return ((Character)c).toString().toLowerCase();  
  28.         // 只取一個發音,如果是多音字,僅取第一個發音  
  29.         return pinyin[0];  
  30.     }  
  31.     /** 
  32.      * 將包含中英文的字串以小寫英文字母的形式返回 
  33.      * @param str 
  34.      * @return 
  35.      */  
  36.     public String getStringPinYin(String str)  
  37.     {  
  38.         StringBuilder sb = new StringBuilder();  
  39.         String tempPinyin = null;  
  40.         for (int i = 0; i < str.length(); ++i)  
  41.         {  
  42.             tempPinyin = getCharacterPinYin(str.charAt(i));  
  43.             sb.append(tempPinyin);  
  44.         }  
  45.         return sb.toString();  
  46.     }  
  47. }  


CustomerComparator.java 
Java程式碼  收藏程式碼
  1. import java.util.Comparator;  
  2. import com.xyz.kjy.db.Customer;  
  3. public class CustomerComparator implements Comparator<Customer> {  
  4.     @Override  
  5.     public int compare(Customer customer0, Customer customer1) {  
  6.         // 按照商家名稱排序  
  7.         String catalog0 = "";  
  8.         String catalog1 = "";  
  9.         if(customer0!=null&&customer0.getStoreName()!=null)  
  10.             catalog0=HanYuUtil.getStringPinYin(customer0.getStoreName());  
  11.         if(customer1!=null&&customer1.getStoreName()!=null)  
  12.             catalog1=HanYuUtil.getStringPinYin(customer1.getStoreName());  
  13.         return catalog0.compareTo(catalog1);  
  14.     }  
  15. }  

影音先鋒電影http://www.iskdy.com/
Customer.java 
Java程式碼  收藏程式碼
  1. public class Customer  {  
  2.     private String storeName;//店名,唯一  
  3.     public String getStoreName() {  
  4.         return storeName;  
  5.     }  
  6.     public void setStoreName(String storeName) {  
  7.         this.storeName = storeName;  
  8.     }  
  9. }  


這裡面用到了一個第三方的包,在附件