Sort an array of numbers in a specific order using an algorithm

49 Views Asked by At

I'm working on a Discord bot that lets users build a base in isometric perspective. For this, I add images of buildings on top of a background with a grid. To make sure the buildings in the front are actually displayed on top of ones in the back, I have to add them in the correct order.

Until now, I just added all numbers in the correct order to an array by hand, but this way I can't expand the grid easily.

The grid is numbered as seen in the image, and I've been looking for a Javascript function that sorts the numbers from back to front (15 -> 14 -> 30 -> 13 -> 29 -> 45 -> ... -> 211) for the past few days, but haven't been able to do it. (I suck at maths lol) Ideally it would be modifiable, so if I expand the grid by one it would start at 16 and go to 226, etc.

Grid