title: How to Sort a Map<String, List<Object>> by the Key with the most values (that are not numeric) assigned to it
body: <p>I have been working with Maps at present and I am baffled by how I can get my program to work effectively.  I can iterate over the map get the keys and values and sort them in alphabetical and reverse alphbetical order quite easily and have used custom comparators for this.  However,  I am now trying to sort the map based on the key with the most values.  The values are a list of objects I have created and can be thought of as this scenario. </p>
  
  <p>There is an Atlas(like a catalog) that has lots of towns (the key of type string). That contains Shops(List).  I want to sort this so that the town with the most shops is displayed first and goes in descending order with the secondary sorting being based on town alphabetically and return a string representing this. </p>
  
  <p>I have used the Comparator interface with seperate classes for each one alphabetically and reverse alphabetically so far and wish to follow the same pattern for learning purposes However this has me completely stumped. </p>
  
  <p>Example:</p>
  
  <pre><code>class Atlas {
  
         Map&lt;String, List&lt;Shop&gt; atlas = new HashMap&lt;String, List&lt;Shop&gt;();
  
         void addShop(Shop shop){
              //if(Atlas already contains){
                get the town and add the shop to it.
              }
              else{
                   add the town as the key and the shop as the value in the list
              }
         }
  
         List&lt;Shop&gt; getAllShopsFromTheGivenTown(String givenTown){
              //if(Atlas contains givenTown){
              return the givenTown from the List. 
              }
              else{
                   //Return an ArrayList emptyList
              }
         }
  
         public String returnAllTownsAndShopsAlphbetically(){
         String tmpString = "";   
  
      List&lt;String&gt; keys = new LinkedList&lt;String&gt;(atlas.keySet());
      TownComparatorAtoZ tc = new TownComparatorAtoZ();
      Collections.sort(keys, tc);
  
      for(String town : keys){
           List&lt;Shop&gt; shops = new LinkedList&lt;Dealer&gt;(atlas.get(town));
           ShopComparatorAtoZ sc = new ShopComparatorAtoZ();
            Collections.sort(shop, sc);
  
          for(Shop shop : shops){
              if(tmpString.isEmpty()){
              tmpString = tmpString + town + ": " + shop.getName();
              }
              else if(tmpString.contains(town)){
              tmpString = tmpString + ", " + shop.getName();
              }
              else{
              tmpString = tmpString + " | " + town + ": " + shop.getName();               }   
          }
      }       
      return tmpString;   
      }
  }
  </code></pre>
  
  <p>As can be seen from above (although not the cleanest and most efficient) returns things alphabetically and will be reformatted into a string builder.  However,  I am wondering how I can use a comparator to achieve what I am after and if someone could provide a code snippet with an explanation of what it actually does I would be grateful as its more about understanding how to do it not just getting a copy and pasted lump of code but need to see if visually in code to understand it. </p>
  
  <p>SO output I want to be something like</p>
  
  <p>manchester: m&amp;s, h&amp;m, schuch | birmingham: game, body shop | liverpool: sports</p>