Here you can find the source of sortByValue(final Map
public static List<Integer> sortByValue(final Map<Integer, Double> map)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; public class Main { public static List<Integer> sortByValue(final Map<Integer, Double> map) { List<Integer> list = new ArrayList<Integer>(); list.addAll(map.keySet());/*from w w w. j av a 2s . c o m*/ Collections.sort(list, new Comparator<Object>() { @SuppressWarnings("unchecked") public int compare(Object o1, Object o2) { Object v1 = map.get(o1); Object v2 = map.get(o2); return ((Comparable<Object>) v1).compareTo(v2); } }); Collections.reverse(list); // if commented, asc return list; } }