中英文混合排序


  • 博客分类:

好久没有写博客了,期间在做桌面云,被要求保密哈。不过最近在写一个android程序,需要实现一个中英文混合排序。不说了,直接代码; 
HanYuUtil.java 

伦理片 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.   
  8. public class HanYuUtil {  
  9.     /** 
  10.      * 将单个字符(包括单个汉字或者单个英文字母)转换为小写字母 
  11.      * @param c 
  12.      * @return 
  13.      */  
  14.     public String getCharacterPinYin(char c)  
  15.     {  
  16.         String[] pinyin=null;  
  17.         HanyuPinyinOutputFormat format =new HanyuPinyinOutputFormat();  
  18.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  19.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//汉字没有声调  
  20.         format.setVCharType(HanyuPinyinVCharType.WITH_V);  
  21.         try{  
  22.             pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);  
  23.         }catch(BadHanyuPinyinOutputFormatCombination e){  
  24.             e.printStackTrace();  
  25.         }  
  26.         // 如果c不是汉字,toHanyuPinyinStringArray会返回null  
  27.         if (pinyin == null)  
  28.             return ((Character)c).toString().toLowerCase();  
  29.         // 只取一个发音,如果是多音字,仅取第一个发音  
  30.         return pinyin[0];  
  31.     }  
  32.     /** 
  33.      * 将包含中英文的字符串以小写英文字母的形式返回 
  34.      * @param str 
  35.      * @return 
  36.      */  
  37.     public String getStringPinYin(String str)  
  38.     {  
  39.         StringBuilder sb = new StringBuilder();  
  40.         String tempPinyin = null;  
  41.         for (int i = 0; i < str.length(); ++i)  
  42.         {  
  43.             tempPinyin = getCharacterPinYin(str.charAt(i));  
  44.             sb.append(tempPinyin);  
  45.         }  
  46.         return sb.toString();  
  47.     }  
  48. }  


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

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


这里面用到了一个第三方的包,在附件
在MySQL中,如果要按照英文字母的顺序进行排序,可以使用ORDER BY子句和COLLATE关键字来实现。COLLATE关键字用于指定排序规则,可以选择不同的排序规则来满足不同的需求。 例如,如果要按照英文字母的顺序从A到Z进行排序,可以使用以下语句: SELECT * FROM 表名 ORDER BY 列名 COLLATE utf8_general_ci; 其中,utf8_general_ci是一种常用的排序规则,它会将英文字母按照字母顺序进行排序。 如果要按照不区分大小写的方式进行排序,可以使用utf8_general_ci规则。如果要按照区分大小写的方式进行排序,可以使用utf8_bin规则。 总结起来,要在MySQL中按照英文字母的顺序进行排序,可以使用ORDER BY子句和COLLATE关键字,并选择合适的排序规则。 #### 引用[.reference_title] - *1* [mysql实现首字母从A-Z排序](https://blog.csdn.net/weixin_34000916/article/details/93186547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [mysql中英文拼音首字母获取及排序](https://blog.csdn.net/qq_38403662/article/details/105372510)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySQL按照汉字拼音A-Z排序或者汉字拼音和英文字母混合A-Z排序](https://blog.csdn.net/weixin_44490662/article/details/112985035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值