I'm making a top down game and needed a way to render objects in the correct order based on their y-axis position. After a day of trying to hard code it, I ended up using Collections.sort() and it works perfectly, but I don't understand why it does. The docs say it sorts by "natural ordering", but I don't understand what that actually is. Here is the code:

My question is, how is the gameObjectList actually being sorted, and since each object has an x, y, width, and height, how is it seemingly sorting by the y-position of the objects without my specifying to sort that way?

If that is indeed so, then Collections.sort() will not do anything. It will not alter the ordering of the original list, because the sorting algorithm is stable (i.e. does not change the order of equal elements - with equal meaning that o1.compareTo(o2) == 0).

Well I guess I'll be back when it breaks lol. I'll play around with compareTo() and try to make it actually compare y values. All I know is for now, it's all rendering in the correct order ¯¯¯\_(ツ)_/¯¯¯ Thanks all

Well yep I'm an idiot! The players are only 1 x 1, and every tile is also 1 x 1, so when the player was "in front" of the objects it looked like it was rendering correctly since they didn't overlap. So I messed around with the compareTo() method but couldn't really get it working correctly. I tried variations of this: (dont mind it being commented out, just did that to get the game back to the original state):

Making the comparator > produces the same results, kinda like it's rendering everything in wrong order. But I'm gonna mess around some more with it and see what I can do. I have a hunch, but it's pure speculation, that is has to do with this code:

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