Java List<Map<String, Object>>排序


一、背景

在开发过程中,我们经常会遇到List<Map<String, Object>>格式的数据,需要根据Map集合的某一键值进行排序,我们可以使用Comparator工具类对其排序。


二、List<Map<String, Object>>排序

废话不多说,直接上代码。

Lambda表达式方式排序:

package com.cw.tsb.test.sort;

import java.util.*;

/**
 * @author Davis
 * @Date 2023-03-10 8:42 下午
 */
public class SortUtil {

    public static void main(String[] args) {
        List<Map<String,Object>> lists = getList(6);

        String sort = "desc";
        String key = "score";
        System.out.println("------------ 排序前 ------------");
        print(lists, key);

        //sortIntMap(lists, sort, key);
        sortDoubleMap(lists, sort, key);

        System.out.println("------------ 排序后 ------------");
        print(lists, key);
    }

    private static void print(List<Map<String,Object>> lists, String key){
        for(int i=0;i<lists.size();i++){
            System.out.println(key + " = " + lists.get(i).get("score"));
        }
    }
    
    private static List<Map<String,Object>> getList(int count){
        List<Map<String,Object>> lists = new ArrayList<>();
        for(int i=0;i<count;i++){
            Map<String, Object> map = new HashMap<>();
            map.put("age", 10 + i);
            map.put("score", 10D + (double)i);
            if(i%2 == 0){
                map.put("age", 100 + i);
                map.put("score", 100D + (double)i);
            }
            lists.add(map);
        }
        return lists;
    }

    /**
     * 对List<Map<String,Object>>中int类型字段进行排序
     * @param lists
     * @param sort 排序,asc/desc
     * @param key 排序字段
     * @return
     */
    public static List<Map<String,Object>> sortIntMap(List<Map<String, Object>> lists, String sort, String key){
        if(sort.equals("asc")){
            lists.sort(Comparator.comparingInt(o -> Integer.parseInt(o.get(key).toString())));
        } else {
            Collections.sort(lists, (o1, o2) -> Integer.parseInt(o2.get(key).toString()) - Integer.parseInt(o1.get(key).toString()));
        }
        return lists;
    }

    /**
     * 对List<Map<String,Object>>中double类型字段进行排序
     * @param lists
     * @param sort 排序,asc/desc
     * @param key 排序字段
     * @return
     */
    public static List<Map<String,Object>> sortDoubleMap(List<Map<String, Object>> lists, String sort, String key){
        if(sort.equals("asc")){
            lists.sort(Comparator.comparingDouble(o -> Double.parseDouble(o.get(key).toString())));
        } else {
            Collections.sort(lists, (o1, o2) -> {
                Double d1 = Double.parseDouble(o2.get(key).toString());
                Double d2 = Double.parseDouble(o1.get(key).toString());
                if(d1 > d2){
                    return 1;
                }
                return -1;
            });
        }
        return lists;
    }
}

普通方式:

package com.cw.tsb.test.sort;

import java.util.*;

/**
 * @author Davis
 * @Date 2023-03-10 8:42 下午
 */
public class SortUtil {

    public static void main(String[] args) {
        List<Map<String,Object>> lists = getList(6);

        String sort = "desc";
        String key = "score";
        System.out.println("------------ 排序前 ------------");
        print(lists, key);

        //listIntSort(lists, sort, key);
        listDoubleSort(lists, sort, key);

        System.out.println("------------ 排序后 ------------");
        print(lists, key);
    }

    private static void print(List<Map<String,Object>> lists, String key){
        for(int i=0;i<lists.size();i++){
            System.out.println(key + " = " + lists.get(i).get("score"));
        }
    }
    private static List<Map<String,Object>> getList(int count){
        List<Map<String,Object>> lists = new ArrayList<>();
        for(int i=0;i<count;i++){
            Map<String, Object> map = new HashMap<>();
            map.put("age", 10 + i);
            map.put("score", 10D + (double)i);
            if(i%2 == 0){
                map.put("age", 100 + i);
                map.put("score", 100D + (double)i);
            }
            lists.add(map);
        }
        return lists;
    }

    /**
     * 对List<Map<String,Object>>中int类型字段进行排序
     * @param lists
     * @param sort 排序,asc/desc
     * @param key 排序字段
     * @return
     */
    public static void listIntSort(List<Map<String, Object>> lists, String sort, String key){
        Collections.sort(lists, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                int i1 = Integer.parseInt(o1.get(key).toString());
                int i2 = Integer.parseInt(o2.get(key).toString());

                int comp = i1 - i2;
                if(sort.equals("desc")){
                    return comp;
                } else {
                    return -comp;
                }
            }
        });
    }


    /**
     * 对List<Map<String,Object>>中double类型字段进行排序
     * @param lists
     * @param sort 排序,asc/desc
     * @param key 排序字段
     * @return
     */
    public static void listDoubleSort(List<Map<String, Object>> lists, String sort, String key){
        Collections.sort(lists, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                double i1 = Double.parseDouble(o1.get(key).toString());
                double i2 = Double.parseDouble(o2.get(key).toString());

                int comp = 1;
                if((i1 - i2) < 0){
                    comp = -1;
                }

                if(sort.equals("desc")){
                    return comp;
                } else {
                    return -comp;
                }
            }
        });
    }
}

执行结果:

------------ 排序前 ------------
score = 100.0
score = 11.0
score = 102.0
score = 13.0
score = 104.0
score = 15.0
------------ 排序后 ------------
score = 11.0
score = 13.0
score = 15.0
score = 100.0
score = 102.0
score = 104.0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值