title: java: sort hashmap by keys and values
body: <p>I want sort a hash map by keys and values. Currently, i have sorted the map by values but the key contains String which i want to sort without disturbing the previously sorted map by values?</p>
  
  <p>map after sorting by values</p>
  
  <pre><code>{Login to new=27, Failed login=27, Impossible=21}
  </code></pre>
  
  <p>How i want to sort map</p>
  
  <pre><code>{Failed login =27, Login to new =27, Impossible =21}
  </code></pre>
  
  <p>I have tried sorting by values first and then keys but failed in achieving what i needed.</p>
  
  <p>P.S i am very new to java</p>
  
  <p>edit: Adding sorting function which i took it from stack overflow</p>
  
  <pre><code>    public static &lt;K, V extends Comparable&lt;? super V&gt;&gt; Map&lt;K, V
      sortByValue(Map&lt;K, V&gt; map) {
      List&lt;Map.Entry&lt;K, V&gt;&gt; list = new LinkedList&lt;&gt;(map.entrySet());
      Collections.sort(list, 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) {
              return (e2.getValue()).compareTo(e1.getValue());
          }
      });
      Map&lt;K, V&gt; result = new LinkedHashMap&lt;&gt;();
      for (Map.Entry&lt;K, V&gt; entry : list) {
          result.put(entry.getKey(), entry.getValue());
      }
      return result;
  }
  </code></pre>