title: Sorting the Map<Key,Value> in descending order based on the value
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java">How to sort a Map&lt;Key, Value&gt; on the values in Java?</a>  </p>
  </blockquote>
  
  
  
  <p>I am using map interface to read from a file and then store the values in that as a key value pair. The file format is as follows</p>
  
  <pre><code> A 34
   B 25
   c 50
  </code></pre>
  
  <p>I will read the datas from this file and store that as a key value pair and then I will display this to the user. My requirement is to display the results in this format </p>
  
  <pre><code>C 50
  A 34
  B 25
  </code></pre>
  
  <p>Thus I need to sort the map in descending order of the value. So that I will be able to display these as my result .. I have read about this and find the below code</p>
  
  <pre><code>static &lt;K,V extends Comparable&lt;? super V&gt;&gt; SortedSet&lt;Map.Entry&lt;K,V&gt;&gt; entriesSortedByValues(Map&lt;K,V&gt; map) {
          SortedSet&lt;Map.Entry&lt;K,V&gt;&gt; sortedEntries = new TreeSet&lt;Map.Entry&lt;K,V&gt;&gt;(
              new Comparator&lt;Map.Entry&lt;K,V&gt;&gt;() {
                  @Override public int compare(Map.Entry&lt;K,V&gt; e1, Map.Entry&lt;K,V&gt; e2) {
                      int res = e1.getValue().compareTo(e2.getValue());
                      return res != 0 ? res : 1; // Special fix to preserve items with equal values
                  }
              }
          );
          sortedEntries.addAll(map.entrySet());
          return sortedEntries;
      }
  </code></pre>
  
  <p>I hope this is gonna sort the values in ascending order, I just want to know whether this approach is correct or some other effective approach will be helpful for me ?</p>