How about storing all related object's UID in an array, sort them, then pick one by one and set it to top?
Probably 2 dimensional array, y(0) = Y coordinate; y(1) = object type ("sprite"/"tilemap"); y(2): UID. This way, you can sort by X axis and get the sort by Y coordinate.
first store each UID in array;
sort array by X axis;
for each array x element {
if( array.at(array.curX, 1) == "SPRITE") {
pick [sprite object] with UID = array.at(array.curX, 2);
move object to top of layer;
} else {
pick [tilemap object] with UID = array.at(array.curX, 2);
move object to top of layer;
}
}[/code:zpdjcf18]