Map 值排序 浮点数

    //升序排序
    public static > Map sortByValueDescending(Map map)
    {
        List> list = new LinkedList>(map.entrySet());
        Collections.sort(list, new Comparator>()
        {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2)
            {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return compare;

              //return -compare;  //降序排序
            }
        });
        Map result = new LinkedHashMap();
        for (Map.Entry entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//调用

Map map = new HashMap<>();

/*

map赋值函数

*/

Map sortByValueDescending = sortByValueDescending(map);
            for (Entry entry : sortByValueDescending.entrySet()) {
                System.out.println(entry.getKey()+"\t"+entry.getValue());
            }

你可能感兴趣的:(Java)