含有中文的字段排序:前中后,上中下

核心代码:

核心代码是JDK的一个工具类,对中文数组的排序

    public static void main(String[] args) {
        List<String> list1 = Arrays.asList("1月上旬", "1月中旬", "1月下旬", "2月上旬", "2月中旬", "2月下旬");
        List<String> list2 = Arrays.asList("1月前旬", "1月中旬", "1月后旬", "2月前旬", "2月中旬", "2月后旬");

        Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
        Arrays.sort(list1.toArray(), cmp);

        for (String s : list1) {
            System.out.println("===list1===" + s);
        }


    }

其中:

 

Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);

 可以提供中文排序的依据


拓展:

对List<T>进行排序  

  • 以对 List<LinkedHashMap<String, Object>>  为例,可对  泛型<T>进行扩展,核心方法是
    sortList
    public static void main(String[] args) {
        List<LinkedHashMap<String, Object>> mapList = fillList();
        sortList(mapList, "月份");
        for (LinkedHashMap<String, Object> map : mapList) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println("==============="+entry.getKey()+"============"+entry.getValue());
            }
        }
    }

    //然后  对  List<LinkedHashMap<string,Object>>  进行填充
    private static List<LinkedHashMap<String,Object>>  fillList(){
        List<LinkedHashMap<String,Object>> list = new ArrayList<>();
        LinkedHashMap<String, Object> map_1 = new LinkedHashMap<>();
        map_1.put("月份","1月上旬");
        map_1.put("地址", ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_2 = new LinkedHashMap<>();
        map_2.put("月份","1月中旬");
        map_2.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_3 = new LinkedHashMap<>();
        map_3.put("月份","1月中旬");
        map_3.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_4 = new LinkedHashMap<>();
        map_4.put("月份","1月下旬");
        map_4.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_5 = new LinkedHashMap<>();
        map_5.put("月份","2月上旬");
        map_5.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_6 = new LinkedHashMap<>();
        map_6.put("月份","2月上旬");
        map_6.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_7 = new LinkedHashMap<>();
        map_7.put("月份","2月中旬");
        map_7.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_8 = new LinkedHashMap<>();
        map_8.put("月份","2月下旬");
        map_8.put("地址",ChineseAddressGenerator.getInstance().generate());
        LinkedHashMap<String, Object> map_9 = new LinkedHashMap<>();
        map_9.put("月份","2月下旬");
        map_9.put("地址",ChineseAddressGenerator.getInstance().generate());

        list.add(map_1);
        list.add(map_2);
        list.add(map_3);
        list.add(map_4);
        list.add(map_5);
        list.add(map_6);
        list.add(map_7);
        list.add(map_8);
        list.add(map_9);
        return list;
    }
    //封装  对 List<LinkedHashMap<string,Object>>  的排序
    private static void  sortList(List<LinkedHashMap<String,Object>> list,String sortKey){
        Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
        Collections.sort(list,new Comparator<LinkedHashMap<String,Object>>(){
            @Override
            public int compare(LinkedHashMap<String, Object> o1, LinkedHashMap<String, Object> o2) {
                String key1 = o1.get(sortKey).toString();
                String key2 = o2.get(sortKey).toString();
                List<String> tempList = Arrays.asList(key1,key2);
                Arrays.sort(tempList.toArray(),cmp);
                String str = StringUtils.join((String[]) tempList.toArray());
                if(Objects.equals(key1,key2)){
                    return 0;
                }else if(str.startsWith(key1)){
                    return -1;
                }else{
                    return 1;
                }

            }
        });
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

PH = 7

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

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

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

打赏作者

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

抵扣说明:

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

余额充值