Search code examples
arraysflashactionscript-3sortingmultidimensional-array

Multidimensional array sort in AS3


What would be the easiest way to do a multi sort in AS3. Something similar to array_multisort() in PHP... like this: sort a multidimentional array using array_multisort

What I have

var COUNTRIES:Array = [["AD","Andorra"],["AE","United Arab Emirates"],["AF","Afghanistan"],["AG","Antigua & Barbuda"],["AI","Anguilla"]];

.. which looped outputs

Andorra
United Arab Emirates
Afghanistan
Antigua & Barbuda
Anguilla

... what I need is to sort it against the second index of each, so I get

Afghanistan
Andorra
Anguilla
Antigua & Barbuda
United Arab Emirates

Solution

  • It's simple:

     COUNTRIES.sortOn("1");
    

    It works because you can access an array index by using a string, just like a property: array["0"]. So sortOn uses the "1" 'property' of each inner array for sorting.