[ Team LiB ] Previous Section Next Section

3.5 Sorting a Simple Array

NN 2, IE 4

3.5.1 Problem

You want to sort an array of numbers or strings.

3.5.2 Solution

To sort an array of numbers from lowest to highest, use the plain sort( ) method of the array object:

myArray.sort( );

This action modifies the order of the items within the array, and its original order cannot be restored unless your scripts have preserved that information elsewhere. Sorting a multidimensional array sorts only the outermost level.

3.5.3 Discussion

You can use the same parameter-less method on an array of string items, but the sorting is performed according to the ASCII values of the string characters. Therefore, if the strings in the array are not homogenous with respect to case, you may receive the array sorted such that all strings starting with uppercase letters sort ahead of those starting with lowercase letters (because ASCII values for uppercase letters are smaller than those for lowercase letters, as shown in Appendix A). For more complex and numeric sorting, however, you need to define a comparison function and invoke it from the sort( ) method.

A comparison function used with array sorts is a very powerful component of the JavaScript language and data manipulation. To invoke the comparison function, pass a reference to the function as the sole parameter of the sort( ) method.

Sorting through a comparison function causes the interpreter to repeatedly send pairs of values from the array to the function. The function should have two parameter variables assigned to it. The job of the function is to compare each pair of values, and return a value of less than zero, zero, or greater than zero, depending on the relationships between the two values:

<0

The second passed value should sort later than the first value.

0

The sort order of the two values should not change.

>0

The first passed value should sort later than the second.

As an example, consider an array consisting of numeric values. If you invoke the sort( ) method without any parameters, the default sorting routine treats the values like strings and sorts them according to their ASCII values, which puts the number 10 sorting earlier than 4 because the first character of 10 is a lower ASCII value than that for 4.

To sort the values in genuine numerical order, you need to create a sorting function that explicitly compares the values as numbers:

function compareNumbers(a, b) {
    return a - b;
}

Invoke the sort through the statement:

myArray.sort(compareNumbers);

Behind the scenes, the JavaScript interpreter repeatedly sends pairs of values from the array to the function. If, during one of the trips to the comparison function the returned value is less than zero, it means that the second value is larger than the first and should be pushed down the sorting order. After rippling through all the values, the array is in the desired sorting order. To change the order of the sorting so that numbers are sorted in descending order, rework the comparison function as follows:

function compareNumbers(a, b) {
    return b - a;
}

For more complex sorting, including how you could sort by the number of characters in array item strings, see Recipe 3.11.

3.5.4 See Also

Recipe 3.11 for sorting arrays of objects based on values of a property in the objects.

    [ Team LiB ] Previous Section Next Section