Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 233

Programming actionscript 3.0
Table of Contents

Advertisement

{
return 1;
}
else
{
return 0;
}
}
trace(names); // output: John Q. Smith,Jane Doe,Mike Jones
names.sort(orderLastName);
trace(names); // output: Jane Doe,Mike Jones,John Q. Smith
The custom sort function
from each element to use for the comparison operation. The function identifier
is used as the sole parameter when calling the
orderLastName
array. The sort function accepts two parameters,
elements at a time. The sort function's return value indicates how the elements should be
sorted:
A return value of -1 indicates that the first parameter,
A return value of 1 indicates that the second parameter,
A return value of 0 indicates that the elements have equal sorting precedence.
The
method is designed for indexed arrays with elements that contain objects.
sortOn()
These objects are expected to have at least one common property that can be used as the sort
key. The use of the
sortOn()
The following example revises the
string. Each object holds both the poet's last name and year of birth.
var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});
You can use the
sortOn()
method defines two parameters,
specified as a string. In the following example,
"
and
born"
Array.NUMERIC
done numerically instead of alphabetically. This is a good practice even when all the numbers
have the same number of digits because it ensures that the sort will continue to behave as
expected if a number with fewer or more digits is later added to the array.
poets.sortOn("born", Array.NUMERIC);
for (var i:int = 0; i < poets.length; ++i)
uses a regular expression to extract the last name
orderLastName()
method for arrays of any other type yields unexpected results.
array so that each element is an object instead of a
poets
method to sort the array by the
and
fieldName
sortOn()
. The
Array.NUMERIC
sort()
and
, because it works on two array
a
b
, precedes the second parameter,
a
, precedes the first,
b
property. The
born
. The
options
fieldName
is called with two arguments,
argument is used to ensure that the sort is
method on the
names
b
.
a
sortOn()
argument must be
Indexed arrays
233
.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents