29.04.2013 Views

ACTIONSCRIPT 3.0

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with arrays<br />

The sortOn() method (Array class only)<br />

The sortOn() method is designed for Array objects with elements that contain objects. These objects are expected to<br />

have at least one common property that can be used as the sort key. The use of the sortOn() method for arrays of any<br />

other type yields unexpected results.<br />

Note: The Vector class does not include a sortOn() method. This method is only available for Array objects.<br />

The following example revises the poets Array so that each element is an object instead of a string. Each object holds<br />

both the poet’s last name and year of birth.<br />

var poets:Array = new Array();<br />

poets.push({name:"Angelou", born:"1928"});<br />

poets.push({name:"Blake", born:"1757"});<br />

poets.push({name:"cummings", born:"1894"});<br />

poets.push({name:"Dante", born:"1265"});<br />

poets.push({name:"Wang", born:"701"});<br />

You can use the sortOn() method to sort the Array by the born property. The sortOn() method defines two<br />

parameters, fieldName and options. The fieldName argument must be specified as a string. In the following<br />

example, sortOn() is called with two arguments, "born" and Array.NUMERIC. The Array.NUMERIC argument is used<br />

to ensure that the sort is done numerically instead of alphabetically. This is a good practice even when all the numbers<br />

have the same number of digits because it ensures that the sort will continue to behave as expected if a number with<br />

fewer or more digits is later added to the array.<br />

poets.sortOn("born", Array.NUMERIC);<br />

for (var i:int = 0; i < poets.length; ++i)<br />

{<br />

trace(poets[i].name, poets[i].born);<br />

}<br />

/* output:<br />

Wang 701<br />

Dante 1265<br />

Blake 1757<br />

cummings 1894<br />

Angelou 1928<br />

*/<br />

Sorting without modifying the original array (Array class only)<br />

Generally, the sort() and sortOn() methods modify an Array. If you wish to sort an Array without modifying the<br />

existing array, pass the Array.RETURNINDEXEDARRAY constant as part of the options parameter. This option directs<br />

the methods to return a new Array that reflects the sort and to leave the original Array unmodified. The Array returned<br />

by the methods is a simple Array of index numbers that reflects the new sort order and does not contain any elements<br />

from the original Array. For example, to sort the poets Array by birth year without modifying the Array, include the<br />

Array.RETURNINDEXEDARRAY constant as part of the argument passed for the options parameter.<br />

The following example stores the returned index information in an Array named indices and uses the indices array<br />

in conjunction with the unmodified poets array to output the poets in order of birth year:<br />

Last updated 4/22/2013<br />

35

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

Saved successfully!

Ooh no, something went wrong!