title: Sorting the Map in descending order of values
body: <p>I have been trying to sort the map in descending order of <code>values</code> but my existing code is sorting in descending order of <code>keys</code>. how do I sort in descending order of values?
  My code:</p>
  
  <pre><code>System.out.println("Unsorted Map:" +merger);
  Map&lt;Integer, Double&gt; Sortedmerger = SortByValue(merger); 
  System.out.println("Sorted Map: "+ Sortedmerger);
  
  public static TreeMap&lt;Integer, Double&gt; SortByValue  (Map&lt;Integer, Double&gt; map) {
  ValueComparator vc =  new ValueComparator(map);
  TreeMap&lt;Integer, Double&gt; sortedMap = new TreeMap&lt;Integer, Double&gt;(Collections.reverseOrder());
          sortedMap.putAll(map);
          return sortedMap;
      }   
  
  class ValueComparator implements Comparator&lt;Double&gt; {
  
      Map&lt;Integer, Double&gt; map;
  
      public ValueComparator(Map&lt;Integer, Double&gt; base) {
          this.map = base;
      }
  
      public int compare(Double a, Double b) {
          if (map.get(a) &gt;= map.get(b)) {
              return -1;
          } else {
              return 1;
          } // returning 0 would merge keys 
      }
  }
  </code></pre>
  
  <p>My output:</p>
  
  <pre><code>Unsorted Map:{1=0.53, 2=0.48, 23=0.54, 10=0.47}
  Sorted Map: {23=0.54, 10=0.47, 2=0.48, 1=0.53}
  </code></pre>