sort (Array.sort method)

public sort([compareFunction:Object], [options:Number]) : Array

Sorts the elements in an array. Flash sorts according to Unicode values. (ASCII is a subset of Unicode.)

By default, Array.sort() works as described in the following list:

If you want to sort an array by using settings that deviate from the default settings, you can either use one of the sorting options described in the entry for the options parameter or you can create your own custom function to do the sorting. If you create a custom function, you can use it by calling the sort() method, using the name of your custom function as the first parameter (compareFunction).

Availability: ActionScript 1.0; Flash Player 5 - Array sorting option added in Flash Player 7.

Parameters

compareFunction:Object [optional] - A comparison function used to determine the sorting order of elements in an array. Given the elements A and B, the result of compareFunction can have one of the following three values:

options:Number [optional] - One or more numbers or names of defined constants, separated by the | (bitwise OR) operator, that change the behavior of the sort from the default. The following values are acceptable for the options parameter:

For more information about this parameter, see the Array.sortOn() method.

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:

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;
    }
}
trace("Unsorted:");
//Displays Unsorted:
trace(passwords_array);
//Displays mom:glam,ana:ring,jay:mag,anne:home,regina:silly.
//Writes mom:glam,ana:ring,jay:mag,anne:home,regina:silly
passwords_array.sort(order);
trace("Sorted:");
//Displays Sorted:
trace(passwords_array);
//Displays ana:ring,anne:home,jay:mag,mom:glam,regina:silly.
//Writes ana:ring,anne:home,jay:mag,mom:glam,regina:silly.

See also

| bitwise OR operator, sortOn (Array.sortOn method)


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001366.html