Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If these "elements" are <em>keys</em> you can store them in a <code>TreeMap</code>, which will produce a consistent order based on the <em>natural ordering</em> of the keys. Note you don't need to do much except create a new map with the old map passed to the constructor:</p> <pre><code>Map&lt;String,?&gt; map = ? Map&lt;String,?&gt; orderedMap = new TreeMap&lt;String,?&gt;(map); </code></pre> <p>Then, iterate like normal:</p> <pre><code>for(String key : orderedMap.keys()){ } </code></pre> <p>If your "elements" are <em>values</em>, then you can insert them as keys into a <code>TreeMap</code> keeping track of the original keys, read the sorted order of values as before (basically creating an inverted index):</p> <pre><code>Map&lt;?,String&gt; map = ? Map&lt;String,List&lt;?&gt;&gt; orderedVals = new TreeMap&lt;String,List&lt;?&gt;&gt;(); for(Entry&lt;?,String&gt; map : map.entrySet()){ List&lt;?&gt; keys = orderedVals.get(map.getValue()); if(keys == null){ keys = new ArrayList&lt;?&gt;(); orderedVals.put(map.getValue(), keys); } keys.add(map.getKey()); } // now orderedVals has keys in sorted order for(String val : orderedVals.keys()){ } </code></pre> <p>Of course, if you're not actually using anything related to the fact these things are in a "hashtable" (I read this as something implementing <code>Map</code>), then you can load up a <code>List</code> of your choosing, and sort it:</p> <pre><code>List&lt;String&gt; list = new ArrayList&lt;String&gt;(map.values()); // or use map.keys() Collections.sort(list); </code></pre> <p>If you're not happy with the default sort order for <code>String</code>, feel free to write your own comparator:</p> <pre><code>Collections.sort(list, new Comparator&lt;String&gt;(){ public int compare(String left, String right){ return // your impl } }); </code></pre> <p><code>compare</code> must return a negative integer when the <code>left</code> comes first, 0 if <code>left</code> and <code>right</code> are the same, and a positive integer if <code>right</code> comes first.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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