User Guide
256 ActionScript classes
Example
The following example creates an array of five pets and uses
slice() to populate a new array
that contains only four-legged pets:
var myPets_array:Array = new Array("cat", "dog", "fish", "canary",
"parrot");
var myFourLeggedPets_array:Array = new Array();
var myFourLeggedPets_array = myPets_array.slice(0, 2);
trace(myFourLeggedPets_array); // Returns cat,dog.
trace(myPets_array); // Returns cat,dog,fish,canary,parrot.
The following example creates an array of five pets, and then uses slice() with a negative
start parameter to copy the last two elements from the array:
var myPets_array:Array = new Array("cat", "dog", "fish", "canary",
"parrot");
var myFlyingPets_array:Array = myPets_array.slice(-2);
trace(myFlyingPets_array); // Traces canary,parrot.
The following example creates an array of five pets and uses slice() with a negative end
parameter to copy the middle element from the array:
var myPets_array:Array = new Array("cat", "dog", "fish", "canary",
"parrot");
var myAquaticPets_array:Array = myPets_array.slice(2,-2);
trace(myAquaticPets_array); // Returns fish.
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:
■ 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".
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).