Here you can find the source of sortMapByValue(Map
public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap, final boolean isDesc)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap) { Map<T, Double> sortedMap = new LinkedHashMap<T, Double>(); if (oriMap != null && !oriMap.isEmpty()) { List<Map.Entry<T, Double>> entryList = new ArrayList<Map.Entry<T, Double>>(oriMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<T, Double>>() { public int compare(Entry<T, Double> entry1, Entry<T, Double> entry2) { Double value1 = entry1.getValue(); Double value2 = entry2.getValue(); return value2.compareTo(value1); }// w w w .j a v a 2s . com }); Iterator<Map.Entry<T, Double>> iter = entryList.iterator(); Map.Entry<T, Double> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), Double.parseDouble(tmpEntry.getValue() + "")); } } return sortedMap; } public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap, final boolean isDesc) { Map<T, Double> sortedMap = new LinkedHashMap<T, Double>(); if (oriMap != null && !oriMap.isEmpty()) { List<Map.Entry<T, Double>> entryList = new ArrayList<Map.Entry<T, Double>>(oriMap.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<T, Double>>() { public int compare(Entry<T, Double> entry1, Entry<T, Double> entry2) { Double value1 = entry1.getValue(); Double value2 = entry2.getValue(); if (isDesc) return value2.compareTo(value1); else return value1.compareTo(value2); } }); Iterator<Map.Entry<T, Double>> iter = entryList.iterator(); Map.Entry<T, Double> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } } return sortedMap; } }