the array is two dimensional - each row holds the y coordinate of the object, the UID of the object, and the object type (P for Player, B for Background family, O for Object family).
when you sort an array by the x axis, it will sort each row by what is in the first column (that is why I put the y coordinate in first).
at the start of the Sort function I set the array size to (0,3,1). That clears the array, and leaves room for three columns on each row.
For each object, we push it's y coordinate to the front of the array (which is row 0, column 0). Then we add the UID and type to columns 1 and 2.
once all the objects are in the array, we sort it by the x axis. That will put the lowest y coordinate at the top, and the highest y coordinate at the bottom of the array - which is how we want the objects arranged on the screen.
then we read through the array, bringing each object to the top of the layer. So, in the end, we only loop through each family once, and the final array once.