java汉字排序,stream().sorted汉字排序

网上常见方法(错误):

    @Test
    public  void HanZiSort() {
        ArrayList<CameraDetail> resultList = new ArrayList();
        CameraDetail detail = new CameraDetail();
        detail.setParkName("嘻嘻嘻x");
        resultList.add(detail);
        CameraDetail detail1 = new CameraDetail();
        detail1.setParkName("有有y");
        resultList.add(detail1);
        CameraDetail detail2 = new CameraDetail();
        detail2.setParkName("吱吱吱z");
        resultList.add(detail2);
        CameraDetail detail3 = new CameraDetail();
        detail3.setParkName("呃呃呃e");
        resultList.add(detail3);
        List<CameraDetail>  list = resultList.stream()
                .sorted(Comparator.comparing(CameraDetail::getParkName, (x,y) ->{
                    Collator clt = Collator.getInstance(java.util.Locale.CHINA);
                    return clt.compare(x, y);
                })).collect(Collectors.toList());
        list.forEach(row->
                System.out.println(row.getParkName()));
    }
     /* 结果为:
       	嘻嘻嘻x
        有有y
        吱吱吱z
        呃呃呃e
     */

解决思路:将汉字转为拼音在进行排序
添加依赖

<dependency>
  <groupId>com.belerweb</groupId>
  <artifactId>pinyin4j</artifactId>
  <version>2.5.0</version>
</dependency>
  list = resultList.stream()
        .sorted(Comparator.comparing(CameraDetail::getParkName, (x,y) ->{
        	// ToFirstChar 将汉字首字母转为拼音
            x = ToFirstChar(x).toUpperCase();
            y = ToFirstChar(y).toUpperCase();
            Collator clt = Collator.getInstance();
            return clt.compare(x, y);
        })).collect(Collectors.toList());
 	/**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public static String ToFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String ToPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

汉字转拼音参考了:java实现将汉字转为拼音.

发布了5 篇原创文章 · 获赞 1 · 访问量 251
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览