Recipe 6.10 Sorting or Reversing an Array

6.10.1 Problem

You want to sort the elements of an array.

6.10.2 Solution

Use the sort( ) method. For arrays of objects, you can also use the sortOn( ) method.

6.10.3 Discussion

You can perform a simple sort on an array using the sort( ) method. The sort( ) method, without any parameters, sorts the elements of an array in ascending order. Elements are sorted according to the Unicode code points of the characters in the string (roughly alphabetical for Western European languages). However, the sort is also case-sensitive, and it sorts numbers "alphabetically" instead of numerically. See Recipe 6.11 for details on creating custom sorting algorithms that are case-insensitive.

words = ["tricycle", "relative", "aardvark", "jargon"]; words.sort(  ); trace(words);   // Displays: aardvark,jargon,relative,tricycle

The reverse( ) method reverses the order of the elements in an array:

words = ["tricycle", "relative", "aardvark", "jargon"]; words.reverse(  ); trace(words);   // Displays: jargon,aardvark,relative,tricycle

If you want to sort the elements of an array in descending order, use the sort( ) method followed by the reverse( ) method:

words = ["tricycle", "relative", "aardvark", "jargon"]; words.sort(  ); words.reverse(  ); trace(words); // Displays: tricycle,relative,jargon,aardvark

You can sort arrays of objects using the sortOn( ) method. The sortOn( ) method requires a string parameter specifying the name of the property on which to sort the elements:

cars = new Array(  ); cars.push({make: "Honda",    year: 1997, color: "maroon"}); cars.push({make: "Chrysler", year: 2000, color: "beige"}); cars.push({make: "Mercedes", year: 1985, color: "blue"}); cars.push({make: "Fiat",     year: 1983, color: "gray"}); // Sort the cars array according to the year property of each element. cars.sortOn("year"); for (var i = 0; i < cars.length; i++) {   // Outputs:   // A gray 1983 Fiat   // A blue 1985 Mercedes   // A maroon 1997 Honda   // A beige 2000 Chrysler   trace("A " + cars[i].color + " " + cars[i].year + " " + cars[i].make); }

To sort the elements of an array of objects in descending order, call the reverse( ) method after calling sortOn( ).

Sorted arrays can be useful in many scenarios. For example, if you want to display the elements of an array in a UI component or a text field, you often want to list the elements in alphabetical order.

6.10.4 See Also

The sort( ) and sortOn( ) methods make changes to the order of the original array; they do not return a new array. See Recipe 6.8 to make a separate copy of an array on which you can perform destructive operations. See Recipe 6.11 for details on case-insensitive sorting and numeric sorting, as well as custom sorting in general.



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net