Java Comparator implement to use Map as backend
import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Main { public static void main(String[] args) { HashMap<String, Double> map = new HashMap<String, Double>(); map.put("CSS", 9.0); map.put("HTML", 6.0); map.put("Java", 7.0); map.put("Javascript", 10.0); //from w w w. j a v a 2 s .co m ValueComparator bvc = new ValueComparator(map); TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc); System.out.println("unsorted map: " + map); sorted_map.putAll(map); System.out.println("results: " + sorted_map); } } class ValueComparator implements Comparator<String> { Map<String, Double> base; public ValueComparator(Map<String, Double> base) { this.base = base; } public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return -1; } else { return 1; } } }