Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create a class that has the character and the frequency as data member, and make the class implements <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html" rel="nofollow"><code>Comparable</code></a> interface.</p> <p>There are 2 options here, you can either:</p> <ul> <li><p>Insert all objects into a class that implements <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html" rel="nofollow"><code>List</code></a> interface (e.g. <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html" rel="nofollow"><code>ArrayList</code></a>). Then call <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29" rel="nofollow"><code>Collections.sort(List&lt;T&gt; list)</code></a> on the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html" rel="nofollow"><code>List</code></a>.</p></li> <li><p>Insert all objects into <a href="http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html" rel="nofollow"><code>TreeSet</code></a>. You can obtain the sorted item via <code>iterator()</code>.</p></li> </ul> <p>From the question, it seems that the 2 pieces of data are not members of some existing object in the first place. If any chance that they do, you don't have to create a new class for the character and frequency. You can insert the existing object into a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html" rel="nofollow"><code>List</code></a>, implement a class that extends on <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow"><code>Comparator</code></a> interface, and sort the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/List.html" rel="nofollow"><code>List</code></a> with <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29" rel="nofollow"><code>Collections.sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)</code></a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload