[Javascript] Sort function

Erwin Kodiat Erwin.Kodiat at mitrais.com
Wed Jul 18 20:48:42 CDT 2001


The following was copied from DevEdge JS Reference:

sort
Sorts the elements of an array. 
Method of
 Array
 
Implemented in
 Navigator 3.0, LiveWire 1.0
Navigator 4.0: modified behavior.
 



Syntax
sort(compareFunction)
Parameters
compareFunction
 Specifies a function that defines the sort order. If omitted, the array is
sorted lexicographically (in dictionary order) according to the string
conversion of each element.
 



Description
If compareFunction is not supplied, elements are sorted by converting them
to strings and comparing strings in lexicographic ("dictionary" or
"telephone book," not numerical) order. For example, "80" comes before "9"
in lexicographic order, but in a numeric sort 9 comes before 80.
If compareFunction is supplied, the array elements are sorted according to
the return value of the compare function. If a and b are two elements being
compared, then:



If compareFunction(a, b) is less than 0, sort b to a lower index than a. 

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to
each other, but sorted with respect to all different elements. 

If compareFunction(a, b) is greater than 0, sort b to a higher index than a.

So, the compare function has the following form:

function compare(a, b) {   if (a is less than b by some ordering criterion)
return -1   if (a is greater than b by the ordering criterion)      return 1
// a must be equal to b   return 0}
To compare numbers instead of strings, the compare function can simply
subtract b from a:

function compareNumbers(a, b) {   return a - b}
JavaScript uses a stable sort: the index partial order of a and b does not
change if a and b are equal. If a's index was less than b's before sorting,
it will be after sorting, no matter how a and b move due to sorting.
The behavior of the sort method changed between Navigator 3.0 and Navigator
4.0. 

In Navigator 3.0, on some platforms, the sort method does not work. This
method works on all platforms for Navigator 4.0.
In Navigator 4.0, this method no longer converts undefined elements to null;
instead it sorts them to the high end of the array. For example, assume you
have this script: 


<SCRIPT>a = new Array();a[0] = "Ant";a[5] = "Zebra";
function writeArray(x) {   for (i = 0; i < x.length; i++) {
document.write(x[i]);      if (i < x.length-1) document.write(", ");   }}
writeArray(a);a.sort();document.write("<BR><BR>");writeArray(a);</SCRIPT>
In Navigator 3.0, JavaScript prints: 

ant, null, null, null, null, zebra ant, null, null, null, null, zebra
In Navigator 4.0, JavaScript prints: 

ant, undefined, undefined, undefined, undefined, zebra ant, zebra,
undefined, undefined, undefined, undefined
Examples
The following example creates four arrays and displays the original array,
then the sorted arrays. The numeric arrays are sorted without, then with, a
compare function.

<SCRIPT>stringArray = new
Array("Blue","Humpback","Beluga")numericStringArray = new
Array("80","9","700")numberArray = new Array(40,1,5,200)mixedNumericArray =
new Array("80","9","700",40,1,5,200)
function compareNumbers(a, b) {   return a - b}
document.write("<B>stringArray:</B> " + stringArray.join()
+"<BR>")document.write("<B>Sorted:</B> " + stringArray.sort() +"<P>")
document.write("<B>numberArray:</B> " + numberArray.join()
+"<BR>")document.write("<B>Sorted without a compare function:</B> " +
numberArray.sort() +"<BR>")document.write("<B>Sorted with
compareNumbers:</B> " + numberArray.sort(compareNumbers) +"<P>")
document.write("<B>numericStringArray:</B> " + numericStringArray.join()
+"<BR>")document.write("<B>Sorted without a compare function:</B> " +
numericStringArray.sort() +"<BR>")document.write("<B>Sorted with
compareNumbers:</B> " + numericStringArray.sort(compareNumbers) +"<P>")
document.write("<B>mixedNumericArray:</B> " + mixedNumericArray.join()
+"<BR>")document.write("<B>Sorted without a compare function:</B> " +
mixedNumericArray.sort() +"<BR>")document.write("<B>Sorted with
compareNumbers:</B> " + mixedNumericArray.sort(compareNumbers)
+"<BR>")</SCRIPT>
This example produces the following output. As the output shows, when a
compare function is used, numbers sort correctly whether they are numbers or
numeric strings.

stringArray: Blue,Humpback,BelugaSorted: Beluga,Blue,Humpback
numberArray: 40,1,5,200Sorted without a compare function: 1,200,40,5Sorted
with compareNumbers: 1,5,40,200
numericStringArray: 80,9,700Sorted without a compare function:
700,80,9Sorted with compareNumbers: 9,80,700
mixedNumericArray: 80,9,700,40,1,5,200Sorted without a compare function:
1,200,40,5,700,80,9Sorted with compareNumbers: 1,5,9,40,80,200,700

Erwin Kodiat
Mitrais Software Development Center
Bali, Indonesia
Tel: +62 361 755025
Fax: +62 361 755024
http://www.mitrais.com 

This transmission is for the intended addressee only and is confidential
information.  If you have received this transmission in error, please delete
it and notify the sender.  The contents of this E-mail are the opinion of
the writer only and are not endorsed by MROnly.com Pte Ltd unless expressly
stated otherwise. 

-----Original Message-----
From: Reuben D Budiardja [mailto:reubendb at goshen.edu]
Sent: Wednesday, July 18, 2001 23:26
To: javascript at LaTech.edu
Subject: [Javascript] Sort function



Hello, 
is there somekind of sort function to sort the element of an array, and 
returning array with sorted element in javascript?

Thanks in advance.
Reuben D. Budiardja
_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
http://www.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list