Here you can find the source of sortByValue( final Map
Sorts a Map by value Partially buggy due to http://stackoverflow.com/questions/109383/how-to-sort -a-mapkey-value-on-the-values-in-java/1283722#1283722
Parameter | Description |
---|---|
map | a parameter |
@Deprecated public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortByValue( final Map<K, V> map)
//package com.java2s; //License from project: Open Source License import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { /**/*ww w . j a v a2 s . co m*/ * Sorts a Map by value * * Partially buggy due to * http://stackoverflow.com/questions/109383/how-to-sort * -a-mapkey-value-on-the-values-in-java/1283722#1283722 * * @param map * @return SortedMap by Value */ @Deprecated public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortByValue( final Map<K, V> map) { final List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<K, V>>() { @Override public int compare(final Map.Entry<K, V> op1, final Map.Entry<K, V> op2) { return (op2.getValue()).compareTo(op1.getValue()); } }); return list; } }