title: How to make a generic ValueComparator for sorting multiple HashMaps?
body: <p>I first started working with a single HashMap such as:</p>
  
  <pre><code>private static Map&lt;Broker, Integer&gt; brokerSalesCountList = new HashMap&lt;Broker, Integer&gt;();
  </code></pre>
  
  <p>and used</p>
  
  <pre><code>private static ValueComparator4Broker bvc =  new ValueComparator4Broker(brokerSalesCountList);
  private static TreeMap&lt;Broker,Integer&gt; sortedbrokerSalesCountList  = new TreeMap&lt;Broker,Integer&gt;(bvc);
  
  sortedbrokerSalesCountList.putAll(brokerSalesCountList);
  </code></pre>
  
  <p>Here's my Value Comparator code:</p>
  
  <pre><code>public class ValueComparator4Broker implements Comparator&lt;Broker&gt; {
  
      Map&lt;Broker, Integer&gt; base;
      public ValueComparator4Broker(Map&lt;Broker, Integer&gt; base) {
          this.base = base;
      }
  
      @Override
      public int compare(Broker a, Broker b) {
          if (base.get(a) &gt;= base.get(b)) {
              return -1;
          } else {
              return 1;
          }
      }
  }
  </code></pre>
  
  <p><strong>PROBLEM:</strong> Now I have more than 2 new Hash Maps to be sorted the same way, and rather than duplicating the code for <em>ValueComparator4Broker</em> class, I want to make a single generic and parameterized <em>ValueComparator</em> class which can be used by all new *HashMap*s:</p>
  
  <pre><code>private static Map&lt;Area, Integer&gt; areaBrokerCountList = new HashMap&lt;Area, Integer&gt;();
  private static Map&lt;Area, Integer&gt; areaSalesTotalList = new HashMap&lt;Area, Integer&gt;();
  </code></pre>
  
  <p>I tried something like the following, but the code is erroneous and does not even accept the key object of my HashMap to be substituted for <em>Type</em>. Any help to refine the approach and a code example would be greatly appreciated:</p>
  
  <pre><code>import java.lang.reflect.Type;
  import java.util.Comparator;
  import java.util.Map;
  
  public class ValueComparator implements Comparator&lt;Type&gt; {  
      Map&lt;Type, Integer&gt; base;
      public ValueComparator(Map&lt;Type, Integer&gt; base) {
          this.base = base;
      }
  
      @Override
      public int compare(Type a, Type b) {
          if (base.get(a) &gt;= base.get(b)) {
              return -1;
          } else {
              return 1;
          } // returning 0 would merge keys
      }
  
  }
  </code></pre>