The first line defines an array of random names and traces them to the Output panel.
Then you call the
values
Array.CASEINSENSITIVE
causes the items in the array to be sorted in reverse order (z to a). The search is case-
insensitive;
and
a
comes before
.
a
3.
Select Control > Test Movie to test your ActionScript. The following text is displayed in
the Output panel:
Bob,Dan,doug,bill,Hank,tom
tom,Hank,doug,Dan,Bob,bill
There are five options available in the sort method:
1 or Array.CASEINSENSITIVE (binary = 1)
2 or Array.DESCENDING (binary = 10)
4 or Array.UNIQUESORT (binary = 100)
8 or Array.RETURNINDEXEDARRAY (binary = 1000)
16 or Array.NUMERIC (binary = 10000)
There are three different ways you can define the sort options for an array:
my_array.sort(Array.CASEINSENSITIVE | Array.DESCENDING); // constants
my_array.sort(1 | 2); // numbers
my_array.sort(3); // adding the numbers
Although it might not be immediately obvious, the number values for the sort options are
actually bitwise digits (binary or base 2). The constant value
the numeric value of 1, which also happens to be the binary value of 1. The constant value
has a numeric value of 2 or a binary value of 10.
Array.DECENDING
Working with binary numbers can get confusing. Binary only has two possible values, 1 or 0,
which is why the value 2 is represented as 10. If you want to display the number 3 in binary, it
would be 11 (1+10). The number 4 represented in binary is 100, representing 5 in binary is
101, and so on.
The following ActionScript demonstrates how to sort an array of numeric values in
descending order by using the bitwise AND operator to add the
constants together.
Array.NUMERIC
var scores:Array = new Array(100,40,20,202,1,198);
trace(scores); // 100,40,20,202,1,198
trace(scores.sort()); // 1,100,198,20,202,40
var flags:Number = Array.NUMERIC|Array.DESCENDING;
trace(flags); // 18 (base 10)
trace(flags.toString(2)); // 10010 (binary -- base2)
trace(scores.sort(flags)); // 202,198,100,40,20,1
198
Syntax and Language Fundamentals
method and specify two sort options using the constant
Array.sort()
and
Array.DESCENDING
are treated the same, instead of having a case-sensitive search where
A
. The result of the sort method
Array.CASEINSENSITIVE
Array.DESCENDING
Z
equals
and
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?