I was finally able to solve it with ordering without any hacks. Here's how (if anyone needs it):
1. Get the tiles for each layer (z axis)
2. Get the ones that are free (the ones that do not intersect half way through with other tiles)
3. Sort the free ones right to left, top to bottom and insert them in an array with ordered ones
4. Remove the free ones from the array
5. Repeat step 2 until there are no tiles left.
6. Reverse the array with the ordered tiles.
Works like a charm.