title: How to sort List of Map String in ascending order
body: <p>I have a list of HashMap and I want to sort it in ascending order in Java. </p>
  
  <p>I am using custom Comparator to implement the logic, but its failing. Its just grouping the list based on Map keys. Its not sorting the list entirely.</p>
  
  <p>My Code looks like-</p>
  
  <pre><code>public class ListMap {
  
      public static void main(String[] args) {
          Calendar cal = Calendar.getInstance();
          List&lt;HashMap&lt;String, String&gt;&gt; list = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; 20; i++) {
              int value = randInt(1, 20);
              long timeInMillis = cal.getTimeInMillis();
              HashMap&lt;String, String&gt; mp = new HashMap&lt;&gt;();
              mp.put("" + value, "Values" + value + ":" + timeInMillis);
              list.add(mp);
          }
  
          Collections.sort(list, new Comparator&lt;Map&lt;String, String&gt;&gt;() {
              @Override
              public int compare(Map&lt;String, String&gt; map1,
                      Map&lt;String, String&gt; map2) {
                  String val1 = "";
                  String val2 = "";
                  for (Map.Entry&lt;String, String&gt; entry : map1.entrySet()) {
                      val1 = entry.getKey();
                  }
                  for (Map.Entry&lt;String, String&gt; entry : map2.entrySet()) {
                      val2 = entry.getKey();
                  }
                  return val1.compareTo(val2);
              }
          });
  
          for (Map&lt;String, String&gt; hashMap : list) {
              for (Map.Entry&lt;String, String&gt; entry : hashMap.entrySet()) {
                  System.out.println(entry.getKey() + "/" + entry.getValue());
              }
          }
  
      }
  
      public static int randInt(int min, int max) {
          Random rand = new Random();
          int randomNum = rand.nextInt((max - min) + 1) + min;
  
          return randomNum;
      }
  
  }
  </code></pre>
  
  <p>It prints the output-</p>
  
  <pre><code>10/Values10:1437660285301
  11/Values11:1437660285301
  11/Values11:1437660285301
  11/Values11:1437660285301
  13/Values13:1437660285301
  13/Values13:1437660285301
  15/Values15:1437660285301
  15/Values15:1437660285301
  15/Values15:1437660285301
  16/Values16:1437660285301
  17/Values17:1437660285301
  18/Values18:1437660285301
  18/Values18:1437660285301
  19/Values19:1437660285301
  19/Values19:1437660285301
  2/Values2:1437660285301
  3/Values3:1437660285301
  4/Values4:1437660285301
  6/Values6:1437660285301
  6/Values6:1437660285301
  </code></pre>
  
  <p>It groups the values by key, but I want the entire sorted list like in the above example. </p>
  
  <p>I was expecting 2 and then 3, then 4 and go on.</p>