title: Java Map sort by value
body: <p>I was looking for ways of sorting <code>Map&lt;String, Integer&gt;</code> by values. I found <a href="https://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java">this post</a>, which solved my sorting problem, but not exactly. According to the post, I wrote the following code:</p>
  
  <pre><code>import java.util.*;
  
  public class Sort {
  
      static class ValueComparator implements Comparator&lt;String&gt; {
  
          Map&lt;String, Integer&gt; base;
  
          ValueComparator(Map&lt;String, Integer&gt; base) {
              this.base = base;
          }
  
          @Override
          public int compare(String a, String b) {
              if (base.get(a) &gt;= base.get(b)) {
                  return 1;
              } else {
                  return -1;
              }
          }
      }
  
      public static void main(String[] args) {
          HashMap&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
          ValueComparator vc = new ValueComparator(map);
          TreeMap&lt;String, Integer&gt; sorted = new TreeMap&lt;String, Integer&gt;(vc);
          map.put("A", 1);
          map.put("B", 2);
          sorted.putAll(map);
          for (String key : sorted.keySet()) {
              System.out.println(key + " : " + sorted.get(key)); // why null values here?
          }
          System.out.println(sorted.values()); // But we do have non-null values here!
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>A : null
  B : null
  [1, 2]
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>
  
  <p>As you can see from the output, the <code>get</code> method always returns <code>null</code>. The reason is my <code>ValueComparator.compare()</code> method never returns <code>0</code>, which I've figured out by making <a href="https://stackoverflow.com/questions/13842830/treemapstring-integer-objects-get-method-return-null-value">this post</a>.</p>
  
  <p>Someone suggested in that post the following to solve the <code>null</code> value problem:</p>
  
  <pre><code>        public int compare(String a, String b) {
              if (base.get(a) &gt; base.get(b)) {
                  return 1;
              }else if(base.get(a) ==  base.get(b)){
                  return 0;
              }
              return -1;  
          }
  </code></pre>
  
  <p>I've tested this piece of code and it introduces a key merging problem. In other words, when the values are equal their corresponding keys are merged.</p>
  
  <p>I've also tried the following:</p>
  
  <pre><code>            public int compare(String a, String b) {
                  if (a.equals(b)) return 0;
                  if (base.get(a) &gt;= base.get(b)) {
                      return 1;
                  } else return -1;
              }
  </code></pre>
  
  <p>It does not work either. Some of the values are still <code>null</code>. Besides, this workaround might potentially have logical problems.</p>
  
  <p>Anyone can propose a fully working solution to my problem? I'd like the sort by value function to work and the <code>get</code> method to work at the same time.</p>