Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For starters, you will need to be using an instance of <code>SortedMap</code>. If the map doesn't implement that interface, then it has an undefined/arbitrary iteration order and you can't control it. (Generally this is the case, since a map is a way of associating values with keys; ordering is an auxiliary concern.)</p> <p>So I'll assume you're using <a href="http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html" rel="nofollow">TreeMap</a>, which is the canonical sorted map implementation. This sorts its keys according to a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow">Comparator</a> which you can supply in the constructor. So if you can write such a comparator that determines which is the "lower" of two arbitrary keys (spoiler alert: you can), this will be straightforward to implement.</p> <p>This will, however, only work when sorting by key. I don't know if it makes much sense to sort a map by value, and I'm not aware of any straightforward way to do this. The best I can think of is to write a <code>Comparator&lt;Map.Entry&gt;</code> that sorts on values, call <code>Map.getEntrySet</code> and push all the entries into a list, then call <code>Collections.sort</code> on the list. It's not very elegant or efficient but it should get the job done if performance isn't your primary concern.</p> <p>(Note also that if your keys aren't immutable, you will run into a lot of trouble, as they won't be resorted when externally changed.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    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