java中根据集合中的汉字进行排序(汉字数字)

需要对list集合中的description字段进行排序,description是汉字且包含汉字数字,如下:

new LegLists("来源-西宁", "第三百一十四赛段"),
new LegLists("平安-来源", "第一千三百一十四赛段"),
new LegLists("平安-西宁", "第六十三赛段")

根据第二个字段,第几赛段进行排序。

整体代码:

    public static void main(String[] args) {
        List<LegLists> legLists = Arrays.asList(
                new LegLists("来源-西宁", "第三百一十四赛段"),
                new LegLists("平安-来源", "第一千三百一十四赛段"),
                new LegLists("平安-西宁", "第六十三赛段")

        );
        Collections.sort(legLists, new Comparator<LegLists>() {
            @Override
            public int compare(LegLists o1, LegLists o2) {
                int num1 = extractNumber(o1.getDescription());
                int num2 = extractNumber(o2.getDescription());
                return Integer.compare(num1, num2);
            }

            private int extractNumber(String s) {
                Matcher matcher = Pattern.compile("[零一二三四五六七八九十百千万]+").matcher(s);
                if (matcher.find()) {
                    return chineseNumberToInt(matcher.group());
                }
                return 0;
            }

            private int chineseNumberToInt(String chineseNumber) {
                Map<Character, Integer> chineseToArabic = new HashMap<>();
                chineseToArabic.put('零', 0);
                chineseToArabic.put('一', 1);
                chineseToArabic.put('二', 2);
                chineseToArabic.put('三', 3);
                chineseToArabic.put('四', 4);
                chineseToArabic.put('五', 5);
                chineseToArabic.put('六', 6);
                chineseToArabic.put('七', 7);
                chineseToArabic.put('八', 8);
                chineseToArabic.put('九', 9);
                chineseToArabic.put('十', 10);
                chineseToArabic.put('百', 100);
                chineseToArabic.put('千', 1000);
                chineseToArabic.put('万', 10000);

                int result = 0;
                int temp = 0;
                int lastUnit = 1;

                for (char ch : chineseNumber.toCharArray()) {
                    int num = chineseToArabic.getOrDefault(ch, -1);
                    if (num != -1) {
                        if (num >= 10) { // it's a unit like 十,百,千,万
                            if (temp == 0) {
                                temp = 1;
                            }
                            result += temp * num;
                            temp = 0;
                            lastUnit = num;
                        } else {
                            temp = temp * 10 + num;
                        }
                    }
                }
                result += temp * lastUnit;
                return result;
            }
        });


        for (LegLists lists : legLists) {
            System.out.println(lists);
        }

    }

    static class LegLists {
        /**
         * 赛段名称
         */
        private String legName;

        public LegLists(String legName, String description) {
            this.legName = legName;
            this.description = description;
        }

        /**
         * 对应的是第几赛段(截图文字描述用)
         */
        private String description;

        public String getLegName() {
            return legName;
        }

        public void setLegName(String legName) {
            this.legName = legName;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return "LegLists{" +
                    "legName='" + legName + '\'' +
                    ", description='" + description + '\'' +
                    '}';
        }
    }

下面是我抽出来的工具类,用于提取汉字中的中文数字,并转成数字:

public class ChineseNumber {


    public static int extractNumber(String s) {
        Matcher matcher = Pattern.compile("[零一二三四五六七八九十百千万]+").matcher(s);
        if (matcher.find()) {
            return chineseNumberToInt(matcher.group());
        }
        return 0;
    }

    public static int chineseNumberToInt(String chineseNumber) {
        Map<Character, Integer> chineseToArabic = new HashMap<>();
        chineseToArabic.put('零', 0);
        chineseToArabic.put('一', 1);
        chineseToArabic.put('二', 2);
        chineseToArabic.put('三', 3);
        chineseToArabic.put('四', 4);
        chineseToArabic.put('五', 5);
        chineseToArabic.put('六', 6);
        chineseToArabic.put('七', 7);
        chineseToArabic.put('八', 8);
        chineseToArabic.put('九', 9);
        chineseToArabic.put('十', 10);
        chineseToArabic.put('百', 100);
        chineseToArabic.put('千', 1000);
        chineseToArabic.put('万', 10000);

        int result = 0;
        int temp = 0;
        int lastUnit = 1;

        for (char ch : chineseNumber.toCharArray()) {
            int num = chineseToArabic.getOrDefault(ch, -1);
            if (num != -1) {
                if (num >= 10) { // it's a unit like 十,百,千,万
                    if (temp == 0) {
                        temp = 1;
                    }
                    result += temp * num;
                    temp = 0;
                    lastUnit = num;
                } else {
                    temp = temp * 10 + num;
                }
            }
        }
        result += temp * lastUnit;
        return result;
    }
}

剩下的排序就简单了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糕手慕辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值