Here you can find the source of sortByValues( Map
Parameter | Description |
---|---|
map | a parameter |
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Map<String, BigDecimal> sortByValues( Map<String, BigDecimal> map)
//package com.java2s; import java.math.BigDecimal; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { /***/*from www . j a v a 2 s.c om*/ * * @param map * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Map<String, BigDecimal> sortByValues( Map<String, BigDecimal> map) { List sortedByValueList = new LinkedList(map.entrySet()); Collections.sort(sortedByValueList, new Comparator<Map.Entry<String, BigDecimal>>() { public int compare(Entry<String, BigDecimal> o1, Entry<String, BigDecimal> o2) { return ((Comparable) (o2).getValue()) .compareTo((o1).getValue()); } }); Map<String, BigDecimal> sortedHashMap = new LinkedHashMap<String, BigDecimal>(); for (Iterator it = sortedByValueList.iterator(); it.hasNext();) { Map.Entry<String, BigDecimal> entry = (Map.Entry) it.next(); sortedHashMap.put(entry.getKey(), entry.getValue()); } return sortedHashMap; } }