# ** Sorting 2d arrays Query **

I used to handle this in AS3 (R.I.P) with the sortOn function

Can’t find a similar equivalent in haxe so far - Can anyone fling me a suggestion ?

many cheers !

You should be able to do something like this:

``````var values = [ 3, 2, 5, 17, 1 ];
values.sort (function (a, b) return a - b);
trace (values) // 1,2,3,5,17
``````

oh yes - thanks for that - my current task involves me having to sort through football league arrays - so I need to be able to sort a 2 dimensional array. Not quite sure how to ammend the above code to handle that.

ok, the brain gnashing continues ! x

aha. I have sort of got it , but sorting it as objects - but I still can’t find a way to sort it on two different values ( so points are priority, with goal difference a second factor )

exciting times though !

Use a comparison function like this:

``````function (a, b) {
if (a.points < b.points)
return -1;

if (a.points > b.points)
return 1;

// a.points == b.points

if (a.goals < b.goals)
return -1;

if (a.goals > b.goals)
return 1;

return 0;
}
``````
1 Like