java按汉字首字母排序

时间:2025-03-02 14:41:23

网上有很多汉字排序但是都有小BUG,这里参考了pinyin4j,修复了BUG

一、首先引jar包 

<dependency>
    <groupId></groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>

二、下面代码是个工具类,直接使用即可

package ;


import ;
import ;
import ;
import ;
import ;


import .;
import .;
import .;
import .;
import .;


import ;


/**
 * 汉字拼音工具
 *
 * @author lxy
 */
public class PinyinSortUtil {

    /**
     * 将传入的list,按汉字首字母排序,汉字串会转换成首字母串
     * 如果有英文直接是英文,有符号会自动忽略
     *
     * @param list     集合list
     * @param sortName 需要排序的属性名称
     * @return
     */
    public static <T> List<T> sortListByFstSpell(List<T> list, String sortName) {
        (list, new Comparator<T>() {
            @Override
            public int compare(T t1, T t2) {
                try {
                    Field field1 = ().getDeclaredField(sortName);
                    (true);
                    Field field2 = ().getDeclaredField(sortName);
                    (true);
                    String s1 = ((String) (t1));
                    String s2 = ((String) (t2));
                    if ((s2) > 0) {
                        return 1;
                    } else {
                        return -1;
                    }
                } catch (Exception e) {
                    ();
                    return 0;
                }
            }
        });// new Comparator() 根据需求定义排序 ;
        return list;
    }

    /**
     * 获取汉字串拼音首字母,英文字符不变
     *
     * @param chinese
     * @return
     */
    public static String getFirstSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = ();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        ();
        (HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < ; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = (
                            arr[i], defaultFormat);
                    if (temp != null) {
                        (temp[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    ();
                }
            } else {
                (arr[i]);
            }
        }
        return ().replaceAll("\\W", "").trim();
    }

    /**
     * 将汉字转换成拼音
     *
     * @param hanzi
     * @param separator
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String hanziToPinyin(String hanzi, String separator) {
// 创建汉语拼音处理类
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 输出设置,大小写,音标方式
        ();
        (HanyuPinyinToneType.WITHOUT_TONE);
        String pinyingStr = "";
        try {
            pinyingStr = (hanzi, defaultFormat, separator);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            ();
        }
        return pinyingStr;
    }

    /**
     * @param args
     * @throws NoSuchFieldException
     * @throws SecurityException
     */
    public static void main(String[] args) throws Exception {
        List<CompanyResultVo> list = new ArrayList<CompanyResultVo>();
        CompanyResultVo u = new CompanyResultVo();
        (21L);
        ("中s建设吧");
        (u);

        u = new CompanyResultVo();
        ("中国建设吧");
        (18L);
        (u);

        u = new CompanyResultVo();
        ("啊啊啊啊啊");
        (25L);
        (u);

        u = new CompanyResultVo();
        ("啊啊啊啊");
        (89L);
        (u);

        for (CompanyResultVo user : list) {
            (());
        }
        sortListByFstSpell(list, "companyName");
        ("排序后!!!!!!!!!");
        for (CompanyResultVo user : list) {
            (());
        }
    }
}