[AS3] Oh boy, here we go again, depth handling!

Hello again,

I’m having trouble ._. anyway I have an array of objects with different values, and I’m trying to perform a very specific sort, please look at the two pictures I attached, the first picture should show no errors, the second however… Anyway, the difference between the pictures is that, that in the first picture, the middle tile has a Z value of 4, just like the other 8 around it, on the second picture it has a Z value of 2, which means it should be ‘below’ the others, therefore not be visible like it is. The sort function I’m using now looks like this:

[AS]
private function sortDepth():void {
depth_grid.sort(customDepthSort); // depth_grid contains all the objects
var ilen:uint = depth_grid.length;
for(var i:uint = 0; i < ilen; i++){
this.setChildIndex(depth_grid*, i);
}
}
/* Tile is just an extension of the sprite class which contains more values like Z, etc */
private function customDepthSort(a:Tile, b:Tile):int {

    /* DEFAULT_ELEVATION is the default Z of the tiles, which is set to 3 right now */
var compare:Boolean = (a.z - b.z) &gt; DEFAULT_ELEVATION;
       
if(a.y &lt; b.y) {
    if(compare){
        return 1;
    }
    return -1;
} else if(a.y &gt; b.y) {
    if(compare){
        return 1;
    }
    return 1;
} else {
    if(compare){
        return 1;
    }
    return 0;
}

}
[/AS]

I’ve tested loads of variations, and I’ve been trying my best, but I simply cannot get my head around it, if you don’t understand the above, its currently sorting based on the Y, and the Z values. The lower the Y value, the lower down in the displaylist, generally, but then when the Z comes into play I lose it.

As I’ve not been able to solve this myself, I’d like some guidance,

Thanks for any replies.