Sorting an Array in AS3

Where ‘inY’ is a variable within each movieclip, updating with the items Y value, this will sort them in terms of y and then set their depths correspondingly – boom!

var iA:Array = new Array();

iA.push(theClips);

theClips.inY=theClips.y;

iA.sortOn("inY", Array.NUMERIC);
for (var i:int=0; i<iA.length; i++) {
  addChild(iA[i]);//
}

Much better than the stupid manual sorting I have been doing!

If it’s just numbers or something, you’re not sorting based on properties of objects within but just the items themselves, just use .sort not .sortOn.

var numbers:array = [500,100,200];
numbers.sort(Array.NUMERIC);
// 100, 200, 500
Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑