User Guide
234 Working with Arrays
{
trace(poets[i].name, poets[i].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/
Generally, the sort() and sortOn() methods modify an array. If you wish to sort an array
without modifying the existing array, pass the
Array.RETURNINDEXEDARRAY constant as part
of the
options parameter. This option directs the methods to return a new array that reflects
the sort and to leave the original array unmodified. The array returned by the methods is a
simple array of index numbers that reflects the new sort order and does not contain any
elements from the original array. For example, to sort the
poets array by birth year without
modifying the array, include the
Array.RETURNINDEXEDARRAY constant as part of the
argument passed for the
options parameter.
The following example stores the returned index information in an array named
indices and
uses the
indices array in conjunction with the unmodified poets array to output the poets
in order of birth year:
var indices:Array;
indices = poets.sortOn("born", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
for (var i:int = 0; i < indices.length; ++i)
{
var index:int = indices[i];
trace(poets[index].name, poets[index].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/