title: How to compare the length of words in an input text and sort them according to length value
body: <p>I have a string of words which I want to return sorted according to their length. Some words are repeated more than once, so I am using HashMap to return the words and their count:</p>
  
  <pre><code>Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
  
  for (String s : fragments) {
      if (map.containsKey(s)) {
          map.put(s, map.get(s) + 1);
      } else {
          map.put(s, 1);
      }
  
  }
  </code></pre>
  
  <p>How can I use this to return the length of each word and  sort them from longest to shortest?</p>
  
  <p>Edit:
  For example, if the input is a string</p>
  
  <pre><code>"From the torrent, or the fountain,From the red cliff of the mountain"
  </code></pre>
  
  <p>and I do a simple loop:</p>
  
  <pre><code>for (int i = 0; i &lt; fragments.length; i++) {
      System.out.println(fragments[i]+" " +fragments[i].length());
  }
  </code></pre>
  
  <p>the output is: (full output re:@Pshemo)</p>
  
  <pre><code>from 4
  the 3
  or 2
  the 3
  the 3
  red 3
  cliff 5
  of 2
  the 3
  mountain 8
  </code></pre>
  
  <p>and so on. It's unsorted and the words like "the" repeat each time it sees the word. I want to not repeat the duplicates and sort the words from longest to shortest.</p>
  
  <p>The output I want should be:</p>
  
  <pre><code>mountain 8
  cliff 5
  from 4
  the 3
  red 3
  of 2
  or 2
  </code></pre>