天天看點

JavaScript中關于數組的排序方法解讀

衆所周知,JavaScript是一個很強大的腳本語言,也很類似JAVA,都是面向對象型。

JavaScript中,數組是其核心的對象,當然同為數組,它與其他程式設計語言還是有很多不同的。

JavaScript中,建立數組更友善,更靈活,由于JavaScript本身就是一個弱語言類型的腳本語言,是以我們可以如此聲明:var array = new Array();即可,也可以在建立的時候就指定數組的長度或者數組初始化資料。

在JavaScript中,對于一個數組對象,它自身是有一些可供我們直接使用的方法,這一點也很類似我們的JAVA。

屬性概覽      

index         For an array created by a regular expression match, the zero-based index of the match in the string. 

input         For an array created by a regular expression match, reflects the original string against which the regular expression was matched. 

length       Reflects the number of elements in an array 

prototype  Allows the addition of properties to an Array object. 

方法概覽

concat  Joins two arrays and returns a new array.  

join       Joins all elements of an array into a string. 

pop       Removes the last element from an array and returns that element. 

push      Adds one or more elements to the end of an array and returns that last element added. 

reverse  Transposes the elements of an array: the first array element becomes the last and the last becomes the first. 

shift       Removes the first element from an array and returns that element 

slice       Extracts a section of an array and returns a new array. 

splice     Adds and/or removes elements from an array. 

sort        Sorts the elements of an array. 

toString  Returns a string representing the specified object. 

unshift    Adds one or more elements to the front of an array and returns the new length of the array. 

在JAVA中,數組對象的聲明,必須指明所聲明的資料類型。

屬性

length     聲明的數組對象的長度

方法

clone()     

...其他從Object 繼承的,這裡就不列舉了。

根據我們在實際中,我對JavaScript中的數組對象的sort()方法做進一步的分析:

以下内容來源于JavaScript幫助文檔

sort的文法

sort(compareFunction) 

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. 

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

}

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. 

更多詳情,請參加JavaScript的幫助文檔。

關于數組對象的sort(compareFunction) 方法: 對數組元素進行排序,如果不指定參數,則JAVASCRIPT将元素轉換為字元串,然後按字母順序排序(預設情況下是升序)。如果指定了參數,則該參數必須是自定義的排序函數。根據數組中資料類型的不同,排序可以通過給定的排序函數使用不同的标準。

注意:compareFunction是指自己定義的比較方法名。

經過測試,IE,FF,Chrome所使用的排序算法不盡相同。

繼續閱讀