Android 按中文首字母排序

时间:2022-05-08 07:32:35

按中文字母排序

使用https://github.com/belerweb/pinyin4j


    /**

     * 得到中文首字母缩写
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert.toUpperCase();

    }


public class Demo implements Serializable, Comparable<Demo>{


    private String sortKey;

    public String getSortKey() {
        return sortKey;
    }


    public void setSortKey(String sortKey) {
        this.sortKey = sortKey;
    }

    @Override
    public int compareTo(Demo another) {
        return this.getSortKey().compareTo(another.getSortKey());
    }

}