java通过中文拼音对集合数据或数组进行排序简单工具类

话不多说,直接上代码

/**
 * @Authour chris
 * @Date 2019-07-11 下午8:41
 **/
public class PinyinComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        char c1 = ((String) o1).charAt(0);
        char c2 = ((String) o2).charAt(0);
        return concatPinyinStringArray(
                PinyinHelper.toHanyuPinyinStringArray(c1)).compareTo(
                concatPinyinStringArray(PinyinHelper
                        .toHanyuPinyinStringArray(c2)));
    }
    private String concatPinyinStringArray(String[] pinyinArray) {
        StringBuffer pinyinSbf = new StringBuffer();
        if ((pinyinArray != null) && (pinyinArray.length > 0)) {
            for (int i = 0; i < pinyinArray.length; i++) {
                pinyinSbf.append(pinyinArray[i]);
            }
        }
        return pinyinSbf.toString();
    }
} 
  

 

如有不妥之处,望各位大佬纠正

你可能感兴趣的:(工具类,java,工具)