天天看点

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所使用的排序算法不尽相同。

继续阅读