Note that there are some explanatory texts on larger screens.

plurals
  1. POComparator for TreeSet doesn't work as wanted
    primarykey
    data
    text
    <p>My idea was to sort some entities (which are just some integers) by their layers. I found someone who seemed to have the same problem (I think) but I didn't understand the solution. The layers of each entity is stored in an array. I used a an enumaration for the layers.</p> <pre><code>public enum Layer { DEFAULT, BACKGROUND, FOREGROUND, HUD_0, HUD_1 } </code></pre> <p>So I don't know if I misunderstood the way a comparator works or if i just have problems because the entities are Integers...</p> <pre><code>final Layer[] layers = {Layer.BACKGROUND, Layer.HUD_1, Layer.DEFAULT, Layer.DEFAULT, Layer.HUD_0, Layer.HUD_1}; TreeSet&lt;Integer&gt; sorted = new TreeSet&lt;Integer&gt;(new Comparator&lt;Integer&gt;() { @Override public int compare(Integer entity1, Integer entity2) { //Integer layer1 = layers[entity1].ordinal(); //Integer layer2 = layers[entity2].ordinal(); //return layer1 &lt; layer2 ? -1 : (layer1 &gt; layer2 ? 1 : 0); return layers[entity1].compareTo(layers[entity2]); } }); sorted.add(0);//bg System.out.println(sorted.toString()); sorted.add(4);//hud0 System.out.println(sorted.toString()); sorted.add(2);//def System.out.println(sorted.toString()); sorted.add(3);//def System.out.println(sorted.toString()); sorted.add(1);//hud1 System.out.println(sorted.toString()); sorted.add(5);//hud1 System.out.println(sorted.toString()); </code></pre> <p>This is the output:</p> <pre><code>[0] [0, 4] [2, 0, 4] [2, 0, 4] [2, 0, 4, 1] [2, 0, 4, 1] </code></pre> <p>the last should actually be [2, 3, 0, 4, 1, 5]</p> <p>I read that a TreeSet stores every element just once since it is a Set but I don't know why its using the layer for that and not the entities which i actually want insert.</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. 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