13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

var poets:Array = ["Blake", "cummings", "Angelou", "Dante"];<br />

poets.sort(); // default sort<br />

trace(poets); // output: Angelou,Blake,Dante,cummings<br />

poets.sort(Array.CASEINSENSITIVE);<br />

trace(poets); // output: Angelou,Blake,cummings,Dante<br />

poets.sort(Array.DESCENDING);<br />

trace(poets); // output: cummings,Dante,Blake,Angelou<br />

poets.sort(Array.DESCENDING | Array.CASEINSENSITIVE); // use two options<br />

trace(poets); // output: Dante,cummings,Blake,Angelou<br />

Custom sorting with the sort() method (Array and Vector classes)<br />

In addition to the basic sorting that’s available for an Array object, you can also define a custom sorting rule. This<br />

technique is the only form of the sort() method that is available for the Vector class. To define a custom sort, you<br />

write a custom sort function and pass it as an argum<strong>en</strong>t to the sort() method.<br />

For example, if you have a list of names in which each list elem<strong>en</strong>t contains a person’s full name, but you want to sort<br />

the list by last name, you must use a custom sort function to parse each elem<strong>en</strong>t and use the last name in the sort<br />

function. The following code shows how this can be done with a custom function that is used as a parameter to the<br />

Array.sort() method:<br />

var names:Array = new Array("John Q. Smith", "Jane Doe", "Mike Jones");<br />

function orderLastName(a, b):int<br />

{<br />

var lastName:RegExp = /\b\S+$/;<br />

var name1 = a.match(lastName);<br />

var name2 = b.match(lastName);<br />

if (name1 < name2)<br />

{<br />

return -1;<br />

}<br />

else if (name1 > name2)<br />

{<br />

return 1;<br />

}<br />

else<br />

{<br />

return 0;<br />

}<br />

}<br />

trace(names); // output: John Q. Smith,Jane Doe,Mike Jones<br />

names.sort(orderLastName);<br />

trace(names); // output: Jane Doe,Mike Jones,John Q. Smith<br />

The custom sort function orderLastName() uses a regular expression to extract the last name from each elem<strong>en</strong>t to<br />

use for the comparison operation. The function id<strong>en</strong>tifier orderLastName is used as the sole parameter wh<strong>en</strong> calling<br />

the sort() method on the names array. The sort function accepts two parameters, a and b, because it works on two<br />

array elem<strong>en</strong>ts at a time. The sort function’s return value indicates how the elem<strong>en</strong>ts should be sorted:<br />

A return value of -1 indicates that the first parameter, a, precedes the second parameter, b.<br />

A return value of 1 indicates that the second parameter, b, precedes the first, a.<br />

A return value of 0 indicates that the elem<strong>en</strong>ts have equal sorting preced<strong>en</strong>ce.<br />

Last updated 6/6/2012<br />

34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!