zの値に応じてソートを行うサンプル。

80×80サイズのSpriteをランダムな座標に20個配置。
ソート前は前後関係がステージへ追加した順(zの値に無関係)となっていますが、
z_sortボタンをクリックするとzの値に即した前後関係に整列されます。
以下ソース。(Flash IDEにて作成)
stop();
/* 使い回しする配列 */
var _objs:Array = new Array();
/* イベント登録(ボタンはステージに配置済み) */
_sortBtn.addEventListener(MouseEvent.CLICK, _zsort);
_resetBtn.addEventListener(MouseEvent.CLICK, _reset);
/* 初回リセット */
_reset();
/**
* リセット
* @param e
*/
function _reset(e:MouseEvent = null):void
{
var aObj:Sprite;
/* クリア処理 */
while(0 < _objs.length)
{
aObj = _objs.pop();
removeChild(aObj);
aObj = null;
}
/* オブジェクトを20個作成 */
for (var i:uint = 0; i < 20; i++)
{
/* オブジェクト作成 */
aObj = new Sprite();
aObj.graphics.beginFill(Math.random() * 0xffffff, 1.00);
aObj.graphics.drawRect( -80, -80, 160, 160);
aObj.graphics.endFill();
aObj.x = Math.random() * stage.stageWidth;
aObj.y = Math.random() * stage.stageHeight;
aObj.z = Math.random() * 2000;
addChildAt(aObj, 0);
/* 配列にpush */
_objs.push(aObj);
}
}
/**
* Zソート
* @param e
*/
function _zsort(e:MouseEvent = null):void
{
/* 比較関数を引数に渡し配列をカスタムソート */
_objs.sort(_compareFunction);
/* 再配置 */
for (var i:int = 0; i < 20; i++)
{
addChildAt(_objs[i], 0);
}
}
/**
* 比較関数
* @param a
* @param b
* @return
*/
function _compareFunction(a:DisplayObject , b:DisplayObject):Number
{
/* zの値を比較(zの値が小さい順に並ぶようにする) */
return a.z - b.z;
}
Useful info. Fortunate me I discovered your site by chance, and I am surprised why this coincidence did not happened earlier! I bookmarked it.