Sorting Descending order: Java Map

前端 未结 5 1872
不思量自难忘°
不思量自难忘° 2020-12-05 04:28

What I want to do is sort a map by value. I went over many questions that are available on the stackoverflow site and found out following solution that does what I want but

5条回答
  •  庸人自扰
    2020-12-05 04:37

    You could simply invert the return value of your compare method by adding a minus sign at the beginning:

    return -((Integer) map.get(o2)).compareTo((Integer) map.get(o1));
    

提交回复
热议问题