I've used a similar method for for sorting tiles in my isometric experiment except that instead of sorting the whole list I surgically insert the tile in its correct position using Collections.binarySearch()

// Methodspublicvoidadd(ISOObjectobj) {if (arrayList.size() <= 0) {// If there are no other objects in the list ( no need to sort )arrayList.add(obj); }// adds an object to the array list at its correct position// ( depends on z value )Comparator<ISOObject> c = newzComparator();intindex;index = Collections.binarySearch(arrayList, obj, c);if (index < 0)index = -(index + 1);// index is a negative value of the position it would have taken// if it doesn't fit between any two other objects// more info in javadocs about Collections.binarySearch()arrayList.add(index, obj); }

Populating and sorting a list every frame sounds needlessly expensive to me. You should consider updating such a list only when needed, which I guess would be far less often than every frame.If you are using OpenGL and you don't need the list for game logic but only for rendering, I'd let the gpu handle that by using a 3d vector for vertex positions, which will automagically render everything in order. Otherwise you should organise your entities in layers as was mentioned earlier. Assuming you use integer depth values this can be as easy as having a

1

TreeMap<Integer, List<Entity>

where each List<Entity> is a layer. To render you just iterate over the map. Of course you should not have too many different depth values.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org