Map集合按某个属性排序

 集合按照“xiangMuShu”这个属性进行排序,示例代码如下:

 List> listMap = new ArrayList>();

        TreeMap map1 = new TreeMap();
        map1.put("xiangMuShu", 84.3);
        map1.put("fenLeiName", "电子");
        TreeMap map2 = new TreeMap();
        map2.put("xiangMuShu", 160.6);
        map2.put("fenLeiName", "中医药");
        TreeMap map3 = new TreeMap();
        map3.put("xiangMuShu", 55.2);
        map3.put("fenLeiName", "农林");
        listMap.add(map1);
        listMap.add(map2);
        listMap.add(map3);
        Collections.sort(listMap,new Comparator>(){
            @Override
            public int compare(TreeMap o1, TreeMap o2) {
                // TODO Auto-generated method stub
                Double xiangMuShu =  Double.parseDouble(o1.get("xiangMuShu").toString());
                Double xiangMuShu2 =  Double.parseDouble(o2.get("xiangMuShu").toString());
                return xiangMuShu2.compareTo(xiangMuShu);
            }}
        );
        
        for (TreeMap treeMap : listMap) {
            System.out.println(treeMap.get("fenLeiName")+":"+treeMap.get("xiangMuShu"));
        }

原文:https://blog.csdn.net/gtlishujie/article/details/79282061

你可能感兴趣的:(Map集合按某个属性排序)