针对List《Map《String,Object》》多条件组合排序

本文介绍了一种对List<Map>集合中元素按多个字段进行排序的方法,通过示例代码展示了如何首先按score字段升序排序,相同分数时再按id字段升序排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求: 对List<Map<String,Object>> 中map的value进行多字段组合排序

话不多说, 直接上代码:

public static void main(String[] args) {

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id","1");
        map1.put("code","1234");
        map1.put("score",90);
        map1.put("rate","88%");
        list.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id","2");
        map2.put("code","2345");
        map2.put("score",70);
        map2.put("rate","60%");
        list.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("id","3");
        map3.put("code","3456");
        map3.put("score",70);
        map3.put("rate","50%");
        list.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("id","4");
        map4.put("code","4567");
        map4.put("score",80);
        map4.put("rate","90%");
        list.add(map4);
        Map<String, Object> map5 = new HashMap<>();
        map5.put("id","5");
        map5.put("code","5678");
        map5.put("score",88);
        map5.put("rate","80%");
        list.add(map5);

        Collections.sort(list, new Comparator<Map<String,Object>>() {

            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String score1 = String.valueOf(o1.get("score"));
                String score2 = String.valueOf(o2.get("score"));
                int c = score1.compareTo(score2);
                if(c != 0)
                    return c;

                String i1 = String.valueOf(o1.get("id"));
                String i2 = String.valueOf(o2.get("id"));
                return i1.compareTo(i2);
            }
        });

        System.out.println(list.toString());

    }

结果:

排序前: [{score=60, code=1234, rate=88%, id=1}, {score=70, code=2345, rate=60%, id=2}, {score=70, code=3456, rate=50%, id=3}, {score=60, code=4567, rate=90%, id=4}, {score=88, code=5678, rate=80%, id=5}]
排序后: [{score=60, code=1234, rate=88%, id=1}, {score=60, code=4567, rate=90%, id=4}, {score=70, code=2345, rate=60%, id=2}, {score=70, code=3456, rate=50%, id=3}, {score=88, code=5678, rate=80%, id=5}]

先对score进行升序排序, 相同分数则对id进行升序排序.

若想进行降序排序, 则只需把compareTo两边的参数互换即可; 

仅供参考!

假设有一个list对象,其中每个元素都是一个map对象,map中包含了多个key-value对。现在的问题是需要对list进行排序排序顺序需要满足多个条件的组合,如何实现呢? 首先需要明确的是,list中的每个元素都是map对象,而且map对象中的value值是object类型,可能是各种类型的数据。因此,在进行多条件排序时,需要考虑到不同类型之间的比较规则。 解决方案如下: 1. 定义一个比较器类,实现Comparator接口,重载其中的compare方法。该方法用于两个map对象之间的比较,并根据比较结果返回一个int值,用于排序。 2. 在比较器类中定义多个成员变量,用于记录排序的多个条件,例如: private String[] orderBy = {"age", "name", "salary"}; // 定义排序的字段 private Boolean[] isAsc = {false, true, true}; // 定义每个字段是否升序排序 3. 在compare方法中,逐一比较两个map对象中指定的多个字段,并根据isAsc数组中记录的排序方式进行排序。例如: for (String field : orderBy) { Object o1 = m1.get(field); Object o2 = m2.get(field); if (o1 == null || o2 == null) { continue; } if (o1 instanceof Integer) { Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; int compare = i1.compareTo(i2); if (compare != 0) { return isAsc[index] ? compare : -compare; } } else if (o1 instanceof String) { String s1 = (String) o1; String s2 = (String) o2; int compare = s1.compareTo(s2); if (compare != 0) { return isAsc[index] ? compare : -compare; } } // other types } 4. 在使用Collections.sort方法对list进行排序时,指定比较器类即可。例如: Collections.sort(list, new MyComparator()); 通过上述方案,可以实现对list中的map对象按指定的多个字段进行排序排序方式可以是升序或降序。对于不同类型的数据,可以分别进行对应的比较规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值