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

Create successful ePaper yourself

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

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

Working with arrays<br />

Sorting an array<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

There are three methods—reverse(), sort(), and sortOn()—that allow you to change the order of an indexed<br />

array, either by sorting or reversing the order. All of these methods modify the existing array. The following table<br />

summarizes these methods and their behavior for Array and Vector objects:<br />

Method Array behavior Vector behavior<br />

reverse() Changes the order of the elem<strong>en</strong>ts so that the last elem<strong>en</strong>t becomes the<br />

first elem<strong>en</strong>t, the p<strong>en</strong>ultimate elem<strong>en</strong>t becomes the second, and so on<br />

sort() Allows you to sort the Array’s elem<strong>en</strong>ts in a variety of predefined ways,<br />

such as alphabetical or numeric order. You can also specify a custom<br />

sorting algorithm.<br />

sortOn() Allows you to sort objects that have one or more common properties,<br />

specifying the property or properties to use as the sort keys<br />

The reverse() method<br />

The reverse() method takes no parameters and does not return a value, but allows you to toggle the order of your<br />

array from its curr<strong>en</strong>t state to the reverse order. The following example reverses the order of the oceans listed in the<br />

oceans array:<br />

var oceans:Array = ["Arctic", "Atlantic", "Indian", "Pacific"];<br />

oceans.reverse();<br />

trace(oceans); // output: Pacific,Indian,Atlantic,Arctic<br />

Basic sorting with the sort() method (Array class only)<br />

For an Array instance, the sort() method rearranges the elem<strong>en</strong>ts in an array using the default sort order. The default<br />

sort order has the following characteristics:<br />

The sort is case-s<strong>en</strong>sitive, which means that uppercase characters precede lowercase characters. For example, the<br />

letter D precedes the letter b.<br />

The sort is asc<strong>en</strong>ding, which means that lower character codes (such as A) precede higher character codes (such as B).<br />

The sort places id<strong>en</strong>tical values adjac<strong>en</strong>t to each other but in no particular order.<br />

The sort is string-based, which means that elem<strong>en</strong>ts are converted to strings before they are compared (for example,<br />

10 precedes 3 because the string "1" has a lower character code than the string "3" has).<br />

You may find that you need to sort your Array without regard to case, or in desc<strong>en</strong>ding order, or perhaps your array<br />

contains numbers that you want to sort numerically instead of alphabetically. The Array class’s sort() method has an<br />

options parameter that allows you to alter each characteristic of the default sort order. The options are defined by a<br />

set of static constants in the Array class, as shown in the following list:<br />

Array.CASEINSENSITIVE: This option makes the sort disregard case. For example, the lowercase letter b precedes<br />

the uppercase letter D.<br />

Array.DESCENDING: This reverses the default asc<strong>en</strong>ding sort. For example, the letter B precedes the letter A.<br />

Array.UNIQUESORT: This causes the sort to abort if two id<strong>en</strong>tical values are found.<br />

Array.NUMERIC: This causes numerical sorting, so that 3 precedes 10.<br />

The following example highlights some of these options. An Array named poets is created that is sorted using several<br />

differ<strong>en</strong>t options.<br />

Last updated 6/6/2012<br />

Id<strong>en</strong>tical to Array behavior<br />

Sorts the elem<strong>en</strong>ts according to the custom<br />

sorting algorithm that you specify<br />

Not available in the Vector class<br />

33

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

Saved successfully!

Ooh no, something went wrong!