EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 198

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
198 Syntax and Language Fundamentals
The first line defines an array of random names and traces them to the Output panel.
Then you call the
Array.sort() method and specify two sort options using the constant
values
Array.CASEINSENSITIVE and Array.DESCENDING. The result of the sort method
causes the items in the array to be sorted in reverse order (z to a). The search is case-
insensitive;
a and A are treated the same, instead of having a case-sensitive search where Z
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
Array.CASEINSENSITIVE equals
the numeric value of 1, which also happens to be the binary value of 1. The constant value
Array.DECENDING has a numeric value of 2 or a binary value of 10.
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
Array.DESCENDING and
Array.NUMERIC constants together.
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

Table of Contents

Related product manuals