Availability: |
| ||||||||
Property/method value type: | Array object | ||||||||
JavaScript syntax: | - | myArray.sort() | |||||||
- | myArray.sort(aComparator) | ||||||||
Argument list: | aComparator | A function object that will compare two items and returns a flag indicating their order in the sort collating sequence. |
The elements in the array are sorted in place and the sorted array is returned as a result of this method. The argument provides a comparator function to determine the relationship between any two items.
The comparator function is necessary if you want to sort into any order other than alphabetically ascending. You can observe the operation of the comparator by placing document.write() methods into its source text. These will demonstrate how the comparison is called during the sort.
In the example, a comparator function shows how to custom sort items. The example demonstrates sorting by length rather than charset collation sequence. You must make sure the comparator returns one of the following three values:
Negative integer - signifies that the first argument is less than the second.
Zero - Signifies that both arguments are the same.
Positive integer - Signifies that the first argument is larger than the second.
You can reverse the sort direction by negating the result returned by this comparator function.
In the example, a more highly optimised comparator is shown as well. The more lengthy version is presented first to illustrate the algorithmic requirements of the comparator, but the second is functionally identical and can be accomplished in one line and therefore the sort is much faster.
The result of this method is the array with its elements sorted according to the comparator.
According to the ECMA standard, this sort may not be stable.
The sort() method is generic and may be applied to non-array objects. However, some objects may not be conducive to sorting like this and the exact behavior may be host implementation dependant in some cases.
The custom comparator is not supported by the WebTV platform as of the Summer 2000 release of the JellyScript interpreter.
It is easy to make the mistake of returning true and false as a result of comparing the two values. For example the following is wrong:
function compare(aValue1, aValue2) { if(aValue1.length <= aValue2.length) { return false; } return true; }
This will not work properly and the resulting sort will be incorrect.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> // Demonstrate array joins myString1 = "This is a sentence made of words."; document.write("Original input string<BR>") document.write(myString1) myArray = myString1.split(" "); document.write("<BR><BR>String split into an array<BR>") displayArrayAsTable(myArray); myArray.sort(compare); document.write("<BR><BR>Array sorted<BR>") displayArrayAsTable(myArray); myString2 = myArray.join(" "); document.write("<BR><BR>Array joined up as a string<BR>") document.write(myString2) // Comparator function function compare(aValue1, aValue2) { if(aValue1.length < aValue2.length) { return -1; } if(aValue1.length > aValue2.length) { return 1; } return 0; } // Optimised comparator function function optimalCompare(aValue1, aValue2) { return (aValue1.length - aValue2.length); } // Display an array in a table function displayArrayAsTable(anArray) { myLength = anArray.length; document.write("<TABLE BORDER=1>"); for(myIndex = 0; myIndex < myLength; myIndex++) { document.write("<TR><TD>"); document.write(myIndex); document.write("</TD><TD>"); document.write(anArray[myIndex]); document.write("</TD><TD>"); document.write(anArray[myIndex].length); document.write("</TD></TR>"); } document.write("</TABLE>") } </SCRIPT> </BODY> </HTML>
See also: | Array.prototype, JellyScript |
Prev | Home | Next |
Array.slice() | Up | Array.splice() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |