User Guide
Array 261
Note: Array.sort() is defined in ECMA-262, but the array sorting options introduced in
Flash Player 7 are Flash-specific extensions to the ECMA-262 specification.
Returns
Array - The return value depends on whether you pass any parameters, as described in the
following list:
■ If you specify a value of 4 or Array.UNIQUESORT for the options parameter and two or
more elements being sorted have identical sort fields, Flash returns a value of 0 and does
not modify the array.
■ If you specify a value of 8 or Array.RETURNINDEXEDARRAY for the options parameter,
Flash returns an array that reflects the results of the sort and does not modify the array.
■ Otherwise, Flash returns nothing and modifies the array to reflect the sort order.
Example
Usage 1: The following example shows the use of Array.sort() with and without a value
passed for
options:
var fruits_array:Array = new Array("oranges", "apples", "strawberries",
"pineapples", "cherries");
trace(fruits_array); // Displays
oranges,apples,strawberries,pineapples,cherries.
fruits_array.sort();
trace(fruits_array); // Displays
apples,cherries,oranges,pineapples,strawberries.
trace(fruits_array); // Writes
apples,cherries,oranges,pineapples,strawberries.
fruits_array.sort(Array.DESCENDING);
trace(fruits_array); // Displays
strawberries,pineapples,oranges,cherries,apples.
trace(fruits_array); // Writes
strawberries,pineapples,oranges,cherries,apples.
Usage 2: The following example uses Array.sort() with a compare function. The entries are
sorted in the form name:password. Sort using only the name part of the entry as a key:
var passwords_array:Array = new Array("mom:glam", "ana:ring", "jay:mag",
"anne:home", "regina:silly");
function order(a, b):Number {
var name1:String = a.split(":")[0];
var name2:String = b.split(":")[0];
if (name1<name2) {
return -1;
} else if (name1>name2) {
return 1;
} else {
return 0;