User Guide
Array 259
sortOn (Array.sortOn method)
public sortOn(fieldName:Object, [options:Object]) : Array
Sorts the elements in an array according to one or more fields in the array. The array should
have the following characteristics:
■ The array is an indexed array, not an associative array.
■ Each element of the array holds an object with one or more properties.
■ All of the objects have at least one property in common, the values of which can be used to
sort the array. Such a property is called a field.
If you pass multiple
fieldName parameters, the first field represents the primary sort field, the
second represents the next sort field, and so on. Flash sorts according to Unicode values.
(ASCII is a subset of Unicode.) If either of the elements being compared does not contain the
field that is specified in the
fieldName parameter, the field is assumed to be undefined, and
the elements are placed consecutively in the sorted array in no particular order.
By default,
Array.sortOn() works in the following way:
■ Sorting is case-sensitive (Z precedes a).
■ Sorting is ascending (a precedes b).
■ The array is modified to reflect the sort order; multiple elements that have identical sort
fields are placed consecutively in the sorted array in no particular order.
■ Numeric fields are sorted as if they were strings, so 100 precedes 99, because "1" is a lower
string value than "9".
Flash Player 7 added the
options parameter, which you can use to override the default sort
behavior. To sort a simple array (for example, an array with only one field), or to specify a sort
order that the
options parameter doesn't support, use Array.sort().
To pass multiple flags, separate them with the bitwise OR (
|) operator:
my_array.sortOn(someFieldName, Array.DESCENDING | Array.NUMERIC);
Flash Player 8 added the ability to specify a different sorting option for each field when you
sort by more than one field. In Flash Player 8, the
options parameter accepts an array of sort
options such that each sort option corresponds to a sort field in the
fieldName parameter.
The following example sorts the primary sort field,
a, using a descending sort; the secondary
sort field,
b, using a numeric sort; and the tertiary sort field, c, using a case-insensitive sort:
Array.sortOn (["a", "b", "c"], [Array.DESCENDING, Array.NUMERIC,
Array.CASEINSENSITIVE]);
NOTE
The fieldName and options arrays must have the same number of elements; otherwise,
the options array is ignored. Also, the
Array.UNIQUESORT and Array.RETURNINDEXDARRAY
options can be used only as the first element in the array; otherwise, they are ignored.