View Javadoc

1   /**
2    * 
3    */
4   package sirius.utils.helpers;
5   
6   import java.util.*;
7   
8   
9   /**
10   * @author Myk Kolisnyk
11   *
12   */
13  public class MapUtils
14  {
15      public static <K, V extends Comparable<? super V>> Map<K, V> 
16          sortByValue( Map<K, V> map )
17      {
18          List<Map.Entry<K, V>> list =
19              new LinkedList<Map.Entry<K, V>>( map.entrySet() );
20          Collections.sort( list, new Comparator<Map.Entry<K, V>>()
21          {
22              public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
23              {
24                  return (o2.getValue()).compareTo( o1.getValue() );
25              }
26          } );
27  
28          Map<K, V> result = new LinkedHashMap<K, V>();
29          for (Map.Entry<K, V> entry : list)
30          {
31              result.put( entry.getKey(), entry.getValue() );
32          }
33          return result;
34      }
35  }