I have a game where I have an ArrayList
of Mobs. Each mob has a position and I want to sort them by their Y value so the lowest mob is rendered last.
I am running LibGDX so the values range from -300 (bottom) to 300 (top).
This is the current setup:
Collections.sort(entities, new Comparator<Entity>() {
@Override
public int compare(Entity e1, Entity e2) {
return String.valueOf(e1.position.y)
.compareTo(String.valueOf(e2.position.y));
}
});
System.out.println("------------------------");
for (Entity e : entities) {
System.out.println(entities.indexOf(e) + ": " + e.entityID);
if (e != null) {
e.draw(sb);
}
}