2 Replies Latest reply on Sep 22, 2008 1:49 PM by Rothrock

# Sorting multidimensional arrays

I have an array containing random dates, each in its own small array of which the first node is the month and the second the year:
var arr:Array = [
[1,2008],
[2,2009],
[6,2009],
[4,2008],
[4,2008]
];

I am trying to write a sorting function to use with arr.sort() that will use the year AND month nodes to arrange the dates chronologically. Can't get it to work though, anyone have any advice?

Cheers all
• ###### 1. Re: Sorting multidimensional arrays
So I solved it myself about 30 seconds after posting. Doh!
I came up with this method which seems to work:
function order(a, b)
{
if(a[1] < b[1])
return -1;
else if(a[1] > b[1])
return 1;
else
{
if(a[0] < b[0])
return -1;
else if(a[0] > b[0])
return 1;
else
return 0;
}
}

Would be interested to know if anyone has a better approach or any thoughts as the whole sorting issue as I am doing a project that might involve a lot of it.

Cheers
• ###### 2. Re: Sorting multidimensional arrays
I would recommend using the date class instead of a multi-array. That way you can easily convert to millisseconds and do a numeric sort.