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:
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 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 as described in the following list:
You can use the options parameter to override the default sort behavior. If you want to sort a simple array (for example, an array with only one field), or if you want 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);
fieldName:Object - A string that identifies a field to be used as the sort value, or an array in which the first element represents the primary sort field, the second represents the secondary sort field, and so on.
options:Object [optional] - One or more numbers or names of defined constants, separated by the | (bitwise OR) operator, that change the sorting behavior. The following values are acceptable for the options parameter:
Code hinting is enabled if you use the string form of the flag (for example, DESCENDING) rather than the numeric form (2).
Array - The return value depends on whether you pass any parameters, as described in the following list:
The following example creates a new array and sorts it according to the name and city fields. The first sort uses name as the first sort value and city as the second. The second sort uses city as the first sort value and name as the second.
var rec_array:Array = new Array();
rec_array.push({name: "john", city: "omaha", zip: 68144});
rec_array.push({name: "john", city: "kansas city", zip: 72345});
rec_array.push({name: "bob", city: "omaha", zip: 94010});
for(i=0; i<rec_array.length; i++){
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// Results:
// john, omaha
// john, kansas city
// bob, omaha
rec_array.sortOn(["name", "city"]);
for(i=0; i<rec_array.length; i++){
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// Results:
// bob, omaha
// john, kansas city
// john, omaha
rec_array.sortOn(["city", "name" ]);
for(i=0; i<rec_array.length; i++){
trace(rec_array[i].name + ", " + rec_array[i].city);
}
// Results:
// john, kansas city
// bob, omaha
// john, omaha
The following array of objects is used by subsequent examples that show how to use the options parameter:
var my_array:Array = new Array();
my_array.push({password: "Bob", age:29});
my_array.push({password: "abcd", age:3});
my_array.push({password: "barb", age:35});
my_array.push({password: "catchy", age:4});
Performing a default sort on the password field produces the following results:
my_array.sortOn("password");
// Bob
// abcd
// barb
// catchy
Performing a case-insensitive sort on the password field produces the following results:
my_array.sortOn("password", Array.CASEINSENSITIVE);
// abcd
// barb
// Bob
// catchy
Performing a case-insensitive, descending sort on the password field produces the following results:
my_array.sortOn("password", Array.CASEINSENSITIVE | Array.DESCENDING);
// catchy
// Bob
// barb
// abcd
Performing a default sort on the age field produces the following results:
my_array.sortOn("age");
// 29
// 3
// 35
// 4
Performing a numeric sort on the age field produces the following results:
my_array.sortOn("age", Array.NUMERIC);
// my_array[0].age = 3
// my_array[1].age = 4
// my_array[2].age = 29
// my_array[3].age = 35
Performing a descending numeric sort on the age field produces the following results:
my_array.sortOn("age", Array.DESCENDING | Array.NUMERIC);
// my_array[0].age = 35
// my_array[1].age = 29
// my_array[2].age = 4
// my_array[3].age = 3
When using the Array.RETURNEDINDEXARRAY sorting option, you must assign the return value to a different array. The original array is not modified.
var indexArray:Array = my_array.sortOn("age", Array.RETURNINDEXEDARRAY);