Advertisement
ivanov_ivan

Compare2

Apr 23rd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. static Map sortByValue(Map map)  { //Descending! Exchange objects to ascending
  2.         List list = new LinkedList(map.entrySet());
  3.         Collections.sort(list, new Comparator(){
  4.             public int compare(Object o1, Object o2) {
  5.                 return ((Comparable) ((Map.Entry) (o2)).getValue())
  6.                         .compareTo(((Map.Entry) (o1)).getValue());
  7.             }
  8.         });
  9.  
  10.         Map result = new LinkedHashMap();
  11.         for (Iterator it = list.iterator(); it.hasNext();) {
  12.             Map.Entry entry = (Map.Entry)it.next();
  13.             result.put(entry.getKey(), entry.getValue());
  14.         }
  15.         return result;
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement