Have you ever wanted to access an array in a different sequence without

changing its current sequence? The ActionScript 3.0 constant, `Array.RETURNINDEXEDARRAY`

, allows you to do that. When you call `Array.sort()`

with `RETURNINDEXEDARRAY`

as a parameter, `sort()`

returns an array of indexes in sorted order. You can then use the indexed array to access the primary array in that sequence. The following example contains an array of students’ names: John, Rachel, Melissa, Calvin. Let’s say this sequence represents their order for the seating chart. Now, however, we would

like to get them in alphabetical order for the grade book. We can do this by calling `students.sort(Array.RETURNINDEXEDARRAY)`

and using the indexed array that it returns. Note that by default, `sort()`

sorts in ascending order. Here’s the code:

var students:Array = ["John", "Rachel", "Melissa", "Calvin"]; var index:Array = students.sort(Array.RETURNINDEXEDARRAY); for(var i:int = 0; i < students.length; i++) { trace(students[index[i]] + ": student index: " + index[i] + " sorted: " + i); }

The example uses a `for()`

loop to step through the returned index array in order, using the variable `i`

, initialized to 0. The `trace()`

statement displays the following information: the student’s name (`students[index[i]]`

); the position of that student in the original array (the value of `index[i]`

); the sorted position of the student, which is the sequence of index, or the value of `i`

. The output looks like this:

Calvin: student index: 3 sorted: 0

John: student index: 0 sorted: 1

Melissa: student index: 2 sorted: 2

Rachel: student index: 1 sorted: 3

And the order of the students array remains unchanged: John, Rachel, Melissa, Calvin.

You can obtain additional sequences by using the OR operator| to combine

`RETURNINDEXEDARRAY`

with other `sort()`

constants. For example, the call to `students.sort()`

in the following code combines `RETURNINDEXEDARRAY`

with the constant `DESCENDING`

, to access students in descending order:

var students:Array = ["John", "Rachel", "Melissa", "Calvin"]; var index:Array = students.sort(Array.RETURNINDEXEDARRAY | Array.DESCENDING); for(var i:int = 0; i < students.length; i++) { trace(students[index[i]] + ": student index: " + index[i] + " sorted: " + i); }

The output for this version looks like this:

Rachel: student index: 1 sorted: 0

Melissa: student index: 2 sorted: 1

John: student index: 0 sorted: 2

Calvin: student index: 3 sorted: 3

For more information on the Array class, see:

http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Array.html