将List<Map<String, Object>>排序,升序、降序

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapSort {
    public static void main(String[] args) throws UnknownHostException, SocketException {

        List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();
        Map<String, Object> map1 = new LinkedHashMap<String, Object>();
        map1.put("count", 2);
        map1.put("name", "abc");
        map1.put("key", "acv");
        listResult.add(map1);
        Map<String, Object> map2 = new LinkedHashMap<String, Object>();
        map2.put("count", 3);
        map2.put("name", "bbc");
        map2.put("key", "bcv");
        listResult.add(map2);
        Map<String, Object> map3 = new LinkedHashMap<String, Object>();
        map3.put("count", 1);
        map3.put("name", "cbc");
        map3.put("key", "ccv");
        listResult.add(map3);
        Map<String, Object> map4 = new LinkedHashMap<String, Object>();
        map4.put("count", 4);
        map4.put("name", "cbc");
        map4.put("key", "ccv");
        listResult.add(map4);

        System.out.println("排序前:");
        for (Map<String, Object> map : listResult) {
            System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
        }

        Collections.sort(listResult, new MapComparatorDesc());

        System.out.println("降序:");
        for (Map<String, Object> map : listResult) {
            System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
        }

        Collections.sort(listResult, new MapComparatorAsc());

        System.out.println("升序:");
        for (Map<String, Object> map : listResult) {
            System.out.println("count:"+map.get("count")+" name:"+map.get("name")+" key:"+map.get("key"));
        }
    }
    static class MapComparatorDesc implements Comparator<Map<String, Object>> {
        @Override
        public int compare(Map<String, Object> m1, Map<String, Object> m2) {
            Integer v1 = Integer.valueOf(m1.get("count").toString());
            Integer v2 = Integer.valueOf(m2.get("count").toString());
            if (v2 != null) {
                return v2.compareTo(v1);
            }
            return 0;
        }

    }
    static class MapComparatorAsc implements Comparator<Map<String, Object>> {
        @Override
        public int compare(Map<String, Object> m1, Map<String, Object> m2) {
            Integer v1 = Integer.valueOf(m1.get("count").toString());
            Integer v2 = Integer.valueOf(m2.get("count").toString());
            if(v1 != null){
                return v1.compareTo(v2);
            }
            return 0;
        }

    }
}

/*运行结果
排序前:
count:2 name:abc key:acv
count:3 name:bbc key:bcv
count:1 name:cbc key:ccv
count:4 name:cbc key:ccv
降序:
count:4 name:cbc key:ccv
count:3 name:bbc key:bcv
count:2 name:abc key:acv
count:1 name:cbc key:ccv
升序:
count:1 name:cbc key:ccv
count:2 name:abc key:acv
count:3 name:bbc key:bcv
count:4 name:cbc key:ccv
*/
时间: 2024-10-05 01:35:43

将List<Map<String, Object>>排序,升序、降序的相关文章

对List&lt;Map&lt;String,Object&gt;&gt; 进行排序

1 package lltse.base.collectiondemo; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Random; 10 11 12 publi

对List&lt;Map&lt;String, Object&gt;&gt;集合排序

private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator<Map<String, String>> mapComprator = new Comparator<Map<String, String>>() { @Override public int compare(Map<String, String>

将Map&lt;String, List&lt;Map&lt;String,Object&gt;&gt;&gt;进行排序

首先我贴上我的代码,刚开始我也不知道怎么排序还写了一些方法,最后请教群里的大神解决了 public Map<String, List<Map<String,Object>>> getGrowList(String id){ List<Map<String, Object>> growList = mentDao.getGrowList(id); SortedMap<String, List<Map<String,Object&g

关于List&lt;Map&lt;String,Object&gt;&gt;集合 的按大小排序

今天写项目,突然遇到了一个问题.程序要求我把一个List<Map<String,Object>>集合按大小排序,卧槽,当时我就懵逼了~        经过研究,功夫不负有心人啊,叼叼哒的我肿么把这个排序搞定了,回头发现,代码也是叼叼哒~醉了醉了~        废话不多说,直接上代码://这是你自己用来接受查询出来的数据的集合. List<Map<String,Object>> listMap1 = new LinkedList<Map<Stri

今天学会了对List&lt;Map&lt;String,Object&gt;&gt;的排序

Collections.sort(dyList, new Comparator<Map<String, Object>>() { public int compare(Map<String, Object> arg0, Map<String, Object> arg1) { int map1value=0; int map2value=0; if(arg0.get("getlicenseDate")!=null&&!arg

MyBatis 返回Map&lt;String,Object&gt;类型

<resultMap type="map" id="stringMap"> <result property="contentIntr" column="contentIntr" javaType="string" /> </resultMap> <!-- 导出所有数据 --> <select id="exportAll" resul

javaBean与Map&lt;String,Object&gt;互转

1. 为什么要实现javaBean与Map<String,Object>相互转换? 用过spring的都知道spring的MVC框架中有一个BaseCommandController对象,利用这个对象我们就可以很方便的将从客户端传递过来的参数封装到一个JavaBean对象中去,而不需要我们request.getParameter("name");bean.setName(name);了,从而也简化了不少的工作.如果大家用过BeanUtils.populate的话,就知道,这

List&lt;map&lt;String,Object&gt;&gt;

List<Map<String, Object>> list = sendService.getApproveMsgToMap(); //第一种 //遍历map for (Map<String, Object> m : list) { for (String k : m.keySet()) { System.out.println(k + " : " + m.get(k)); } } //第二种 for (int i = 0; i < list

如何过滤List&lt;Map&lt;String,Object&gt;&gt; 中的重复Map

最近遇到一个问题,就是如何过滤一个List<Map<String,Object>> 中重复的Map,废话不多说,直接上代码. //去除重复的Map //cfArraylist 表示重复的 List<Map<String,Object>> //listMap 表示去重复数据后的 List<Map<String,Object>> Map<String, Map> msp = new HashMap<String, Map