中英文混合排序
阿新 • • 發佈:2019-01-29
倫理片http://www.dotdy.com/
Java程式碼- import net.sourceforge.pinyin4j.PinyinHelper;
- import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
- import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
- import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
-
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
- import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
- public class HanYuUtil {
- /**
- * 將單個字元(包括單個漢字或者單個英文字母)轉換為小寫字母
- * @param c
- * @return
- */
- public String getCharacterPinYin(char c)
- {
- String[] pinyin=null;
-
HanyuPinyinOutputFormat format =new
- format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//漢字沒有聲調
- format.setVCharType(HanyuPinyinVCharType.WITH_V);
- try{
-
pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
- }catch(BadHanyuPinyinOutputFormatCombination e){
- e.printStackTrace();
- }
- // 如果c不是漢字,toHanyuPinyinStringArray會返回null
- if (pinyin == null)
- return ((Character)c).toString().toLowerCase();
- // 只取一個發音,如果是多音字,僅取第一個發音
- return pinyin[0];
- }
- /**
- * 將包含中英文的字串以小寫英文字母的形式返回
- * @param str
- * @return
- */
- public String getStringPinYin(String str)
- {
- StringBuilder sb = new StringBuilder();
- String tempPinyin = null;
- for (int i = 0; i < str.length(); ++i)
- {
- tempPinyin = getCharacterPinYin(str.charAt(i));
- sb.append(tempPinyin);
- }
- return sb.toString();
- }
- }
CustomerComparator.java
Java程式碼
- import java.util.Comparator;
- import com.xyz.kjy.db.Customer;
- public class CustomerComparator implements Comparator<Customer> {
- @Override
- public int compare(Customer customer0, Customer customer1) {
- // 按照商家名稱排序
- String catalog0 = "";
- String catalog1 = "";
- if(customer0!=null&&customer0.getStoreName()!=null)
- catalog0=HanYuUtil.getStringPinYin(customer0.getStoreName());
- if(customer1!=null&&customer1.getStoreName()!=null)
- catalog1=HanYuUtil.getStringPinYin(customer1.getStoreName());
- return catalog0.compareTo(catalog1);
- }
- }
影音先鋒電影http://www.iskdy.com/
Customer.java
Java程式碼
- public class Customer {
- private String storeName;//店名,唯一
- public String getStoreName() {
- return storeName;
- }
- public void setStoreName(String storeName) {
- this.storeName = storeName;
- }
- }
這裡面用到了一個第三方的包,在附件