title: How to sort a TreeSet by Value?
body: <p>I am pretty new to <code>TreeMap</code> and <code>TreeSet</code> and the likes and was wondering how to sort the data structures by value?  I realise with a TreeSet you can sort it into alphabetical order automatically but I want it to order via value?  Any idea on how to do this?</p>
  
  <p>It currently prints like...</p>
  
  <ul>
  <li>aaa: 29</li>
  <li>aaahealthart: 30</li>
  <li>ab: 23</li>
  <li>abbey: 14</li>
  <li>abdomin: 3</li>
  <li>aberdeen: 29</li>
  <li>aberdeenuni: 20</li>
  </ul>
  
  <p>When I want it to print like...</p>
  
  <ul>
  <li>aaahealthart: 30</li>
  <li>aaa: 29</li>
  <li>aberdeen: 29</li>
  <li>ab: 23</li>
  <li>aberdeenuni: 20</li>
  <li>abbey: 14</li>
  <li>abdomin: 3</li>
  </ul>
  
  <p>Here is my method here...</p>
  
  <pre><code>ArrayList&lt;String&gt; fullBagOfWords = new ArrayList&lt;String&gt;();
  public Map&lt;String, Integer&gt; frequencyOne;
  
  public void termFrequency() throws FileNotFoundException{
      Collections.sort(fullBagOfWords);
      Set&lt;String&gt; unique = new TreeSet&lt;String&gt;(fullBagOfWords);
      PrintWriter pw = new PrintWriter(new FileOutputStream(frequencyFile));
      pw.println("Words in Tweets :   Frequency of Words");
      for (String key : unique) {
          int frequency = Collections.frequency(fullBagOfWords, key);
  
          System.out.println(key + ": " + frequency);
          pw.println(key + ": " + frequency);
          }
      pw.close();
      }
  </code></pre>
  
  <p>Thanks for all the help guys.</p>