List<Map<String ,String>> 数据比较后排序

List<Map<String ,String>> 数据比较后排序

方法一:
测试代码:

    List<Map<String,String>> bufferList = new ArrayList<Map<String,String>>();
    List<Map<String,String>> conversationList = new ArrayList<Map<String,String>>();

    Map<String,String> m1 = new HashMap<>();
    m1.put("counter","4.5");
    Map<String,String> m2 = new HashMap<>();
    m2.put("counter","8.5");
    Map<String,String> m3 = new HashMap<>();
    m3.put("counter","9.5");
    Map<String,String> m4 = new HashMap<>();
    m4.put("counter","0.5");
    Map<String,String> m5 = new HashMap<>();
    m5.put("counter","9.5");
    Map<String,String> m6 = new HashMap<>();
    m6.put("counter","11.5");
    Map<String,String> m7 = new HashMap<>();
    m7.put("counter","10.5");

    bufferList.add(m1);
    bufferList.add(m2);
    bufferList.add(m3);
    bufferList.add(m4);
    bufferList.add(m5);
    bufferList.add(m6);
    bufferList.add(m7);

    Map<String,String> m11 = new HashMap<>();
    m11.put("sga","4.5");
    Map<String,String> m21 = new HashMap<>();
    m21.put("sga","8.5");
    Map<String,String> m31 = new HashMap<>();
    m31.put("sga","9.5");
    Map<String,String> m41 = new HashMap<>();
    m41.put("sga","0.5");
    Map<String,String> m51 = new HashMap<>();
    m51.put("sga","11.5");
    Map<String,String> m61 = new HashMap<>();
    m61.put("sga","22.5");
    Map<String,String> m71 = new HashMap<>();
    m71.put("sga","22.5");

    conversationList.add(m11);
    conversationList.add(m21);
    conversationList.add(m31);
    conversationList.add(m41);
    conversationList.add(m51);
    conversationList.add(m61);
    conversationList.add(m71);

    //排序
    Collections.sort(bufferList, new Comparator<Map<String, String>>() {
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            String name1 = o1.get("counter") ;
            String name2 = o2.get("counter") ;
            //return name1.compareTo(name2);

            // 修改之后 增加类型转换 返回比较数据数据
            Double doubleName1 = Double.parseDouble(name1);
            Double doubleName2 = Double.parseDouble(name2);
            return doubleName2.compareTo(doubleName1);
        }
    });
    
    //排序
    Collections.sort(conversationList, new Comparator<Map<String, String>>() {
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            String name1 = o1.get("sga") ;
            String name2 = o2.get("sga") ;
            //return name2.compareTo(name1);

            // 修改之后 增加类型转换 返回比较数据数据
            Double doubleName1 = Double.parseDouble(name1);
            Double doubleName2 = Double.parseDouble(name2);
            return doubleName2.compareTo(doubleName1);
        }
    });

    int bufferCount = bufferList.size();
    int converCount = conversationList.size();
    
    map.put("bufferList", bufferList.size()<=5 ? bufferList.subList(0, bufferCount) : bufferList);
    map.put("conversationList", conversationList.size()<=5 ? conversationList.subList(0, converCount) : conversationList);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值