1 Reply Latest reply on May 17, 2006 11:07 AM by Rothrock

# Problem sorting an multidimensional array

Problem sorting an array.

Flash 7, As2
I have this array with 3 elements. For each element I have 3 fields [name;
win; loose]

I would like to sort the array by field win.

But when win is equal for more then one element what I would like to do is
maintain the win order and add the loose order.

Here's an example:

// Declare the array
var myArray:Array = new Array ();

// Push the elements
myArray.push ({ name:"Regina", win:10, loose:800 });
myArray.push ({ name:"Ana", win:30, loose:500 });
myArray.push ({ name:"John", win:10, loose:100 });

// Sort by loose
// Outputs John / Ana / Regina /

myArray.sortOn ("loose");
trace (myArray[0].name + " / " + myArray[1].name + " / " +
myArray[2].name + " / ");

// Sort by win
// Outputs Ana / Regina / John /

myArray.sortOn ("win", Array.DESCENDING | Array.NUMERIC);
trace (myArray[0].name + " / " + myArray[1].name + " / " +
myArray[2].name + " / ");

What I would like is to have on the final sort is:
// Ana / John / Regina

// win loose loose

Does anyone have any idea in how to achieve this?

Thanks

• ###### 1. Re: Problem sorting an multidimensional array
You have to make your own sorting function. I have a similar one at home but can't remember the syntax exactly off the top o-me-head. So read the help entry for Array.sort() and it will show you how to create your own sort function.

I found mine to be a bit slow and it sped things up a lot if I did a primary sort using sortOn and then called sort(myfunction).