Sunday, August 29, 2010

Working with Arrays in AS3: Part 6

Today we will learn how to sort the elements in an Array.

To sort elements in an Array, use the sort() method. Without any parameters set, this method sorts the elements in ascending order. Elements are sorted according to their Unicode code.

var myArray:Array = ["charlie", "beta", "alpha", "delta"];
myArray.sort();
trace(myArray);
// Output: alpha,beta,charlie,delta

By default, Arrays are sorted using the Array.ASCENDING modificator, but, we can change the way we want the elements to sort. For example, here we sort them in the descending order:

var myArray:Array = ["charlie", "beta", "alpha", "delta"];
myArray.sort(Array.DESCENDING);
trace(myArray);
// Output: delta,charlie,beta,alpha

The sort() method is case sensitive by default. Look:

var myArray:Array = ["charlie", "Beta", "alpha", "delta", "Arial"];
myArray.sort();
trace(myArray);
// Output: Arial,Beta,alpha,charlie,delta

We can turn the sensitivity off, though:

var myArray:Array = ["charlie", "Beta", "alpha", "delta"];
myArray.sort(Array.CASEINSENSITIVE);
trace(myArray);
// Output: alpha,Beta,charlie,delta

If we try to sort numbers, the sorting won't turn out as we need it to, like shown in this example:

var myArray:Array = [1,12,13,8,9,4,3,19,20,25];
myArray.sort();
trace(myArray);
// Output: 1,12,13,19,20,25,3,4,8,9

In this situation, we can use Array.NUMERIC as the parameter for sort() method:

var myArray:Array = [1,12,13,8,9,4,3,19,20,25];
myArray.sort(Array.NUMERIC);
trace(myArray);
// Output: 1,3,4,8,9,12,13,19,20,25

Sometimes it is needed to only sort an Array if each element in it is unique. Use Array.UNIQUESORT for this:

var myArray:Array = ["charlie", "beta", "alpha", "delta", "alpha"];
myArray.sort(Array.UNIQUESORT);
trace(myArray);
// Output: 0, because the Array contains repeating elements

Sometimes it is needed to sort an Array, but also save the previous version of this Array. Array.RETURNINDEXEDARRAY is helpful in such situations. It returns a new Array of indexes of the sorted Array elements:

var myArray:Array = ["charlie", "beta", "alpha", "delta"];
var indArray:Array = myArray.sort(Array.RETURNINDEXEDARRAY);

// We have our old array as it is:
trace(myArray); // Output: charlie,beta,alpha,delta
// And a new array of sorted indexes of elements of the previous array:
trace(indArray); // Output: 2,1,0,3

// When we have the indexes, we can loop through them
// to retrieve the real element values.

var myNewArray:Array = new Array();
for(var i:int = 0; i<myArray.length; i++){
myNewArray.push(myArray[indArray[i]]);
}

// Now we have a new Array that is a sorted version of the previous Array

trace(myArray); // Output: charlie,beta,alpha,delta
trace(myNewArray); // Output: alpha,beta,charlie,delta

You can set multiple modificators for one sort() method. Separate them using the "|" symbol:

var myArray:Array = ["charlie", "Beta", "alpha", "delta"];
myArray.sort(Array.CASEINSENSITIVE | Array.DESCENDING);
trace(myArray);
// Output: delta,charlie,Beta,alpha

But if you simply need to reverse the elements in an Array, use reverse() method.

var myArray:Array = ["charlie", "beta", "alpha", "delta"];
myArray.reverse();
trace(myArray);
// Output: delta,alpha,beta,charlie.

Thanks for reading! In the next tutorial we will continue sorting Array elements.

Related:

Working with Arrays in AS3: Part 1
Working with Arrays in AS3: Part 2
Working with Arrays in AS3: Part 3
Working with Arrays in AS3: Part 4
Working with Arrays in AS3: Part 5
Working with Arrays in AS3: Part 7

2 comments:

Anonymous said...

In the code associated with this line:
"The sort() method is case sensitive by default."

In the output:
// Output: Arial,eta,alpha,charlie,delta

You are missing the "B" for "Beta".

Kirill Poletaev said...

Thanks, fixed.

Post a Comment