|
All of these examples follow the rules explained before, except for the last one, ar4. The statement ar4 = line1.split("") causes an infinite method execution that will eventually crash the browser, or even the operating system, if you are on a 16-bit version of Windows. The reason for this behavior is obvious—JavaScript tries to split the string with an empty string. Everyone knows that an empty string is found an infinite number of times between two characters, and between two empty strings lies another empty string! sort()Luckily for us, the sort() method is built into JavaScript. It sorts the elements of an array. It is optional to supply a sorting function. If one is not supplied, the array is sorted lexicographically (comparison order, dictionary order), according to the string conversion of each element. The general syntax of this method is as follows:
arrayInstance.sort(compareFunction)
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing the strings in lexicographic order. For example, “10” comes before “9” in lexicographic order, but numeric comparison puts 9 before 10. The structure of a comparison function is very specific. First of all, it should have two parameters, one for each element being compared. Secondly, it should return a value. If a and b are the two elements being compared, then:
The basic form of a comparison function is:
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 }
The most simple comparison function sorts numbers:
function compareNumbers(a, b) { return a – b }
JavaScript uses a stable sort, so the relative order of a and b does not change if a and b are equal according to the comparison function. Here are some comparison functions:
// 1. Lexicographic -- built-in // 2. byIncNum (increasing numbers) function byIncNum(a, b) { return a – b } // 3. byFirstChar (lexicographic order of first char only) function byFirstChar(a, b) { a += "" b += "" if (a.charAt(0) < b.charAt(0)) return –1 if (a.charAt(0) > b.charAt(0)) return 1 return 0 }
The following example and its corresponding output in Figure 13-2 should make this topic clear:
<HTML> <HEAD> <TITLE>Sorting arrays</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- // 1. Lexicographic -- built - in // 2. byIncNum (increasing numbers) function incNum(a, b) { return a – b } stringArray = new Array("house", "hose", "chair") numericStringArray = new Array("60", "8", "100") numberArray = new Array(20, 1, 5, –11, 8) mixedNumericArray = new Array("70", "9", "600", 3, 40, 70, 250) function compareNumbers(a, b) { return a – b } document.write("<U><B>stringArray</B></U><BR>") document.write("<B>Original array:</B> " + stringArray.join() +"<BR>") document.write("<B>Sorted by default:</B> " + stringArray.sort() +"<P>") document.write("<U><B>numberArray</B></U><BR>") document.write("<B>Original array:</B> " + numberArray.join() +"<BR>") document.write("<B>Sorted by default:</B> " + numberArray.sort() +"<BR>") document.write("<B>Sorted with compareNumbers:</B> " + numberArray.sort(compareNumbers) +"<P>") document.write("<U><B>numericStringArray</B></U><BR>") document.write("<B>Original array:</B> " + numericStringArray.join()+"<BR>") document.write("<B>Sorted by default:</B> " + numericStringArray.sort() +"<BR>") document.write("<B>Sorted with compareNumbers:</B> " + numericString Array.sort(compareNumbers) +"<P>") document.write("<U><B>mixedNumericArray</B></U><BR>") document.write("<B>Original array:</B> " + mixedNumericArray.join()+"<BR>") document.write("<B>Sorted by default:</B> " + mixedNumericArray.sort() +"<BR>") document.write("<B>Sorted with compareNumbers:</B> " + mixedNumericArray.sort(compareNumbers) +"<BR>") // --> </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
Example 13-1 (ex13-1.htm). The built-insort function is a simple replacement for sorting via pointers (used in many languages that feature pointers). unshift()The unshift() method is actually the opposite of the shift() one. It appends a list of elements to the beginning of an array. Here is the method defined as a prototype of the Array object type, along with an example to demonstrate it:
function unshift() { for (var i = 0; i < unshift.arguments.length; ++i) { if (unshift.arguments[i] == null) break } // i = number of arguments! (remember ++i is executed during last loop) // i holds the number of arguments for (var j = this.length – 1; j >= 0; --j) { this[j + i] = this[j] } // j == –1 // i == number of arguments for (j = 0; j < i; ++j) { this[j] = unshift.arguments[j] } } Array.prototype.unshift = unshift // EXAMPLE //////////////////////////////////////////// var line = new Array("ccc", "ddd", "eee") line.unshift("aaa", "bbb") document.write(line.join(" "))
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |