Array 的方法
    实现方式: JavaScript 1.1, NES 2.0

    JavaScript 1.2: 修改过的行为.

    ECMA 版本: ECMA-262




    用来指定按某种顺序进行排列的函数. 如果省略, 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 a to a lower index than b.
    • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
    • If compareFunction(a, b) is greater than 0, sort b to a lower 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;

    Some implementations of JavaScript implement 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.

    Sort is stable in SpiderMonkey and all Mozilla-based browsers starting with Gecko 1.9 (see bug 224128).

    The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2.

    In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2.

    In JavaScript 1.2, 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:

    var a = [];
    a[0] = "Ant";
    a[5] = "Zebra";
    // assumes a print function is defined
    function writeArray(x)
      for (i = 0; i < x.length; i++)
        if (i < x.length-1)
          print(", ");

    In JavaScript 1.1, JavaScript prints:

    ant, null, null, null, null, zebra
    ant, null, null, null, null, zebra

    In JavaScript 1.2, JavaScript prints:

    ant, undefined, undefined, undefined, undefined, zebra
    ant, zebra, undefined, undefined, undefined, undefined


    Example: Creating, displaying, and sorting an array

    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.

    var stringArray = ["Blue", "Humpback", "Beluga"];
    var numericStringArray = ["80", "9", "700"];
    var numberArray = [40, 1, 5, 200];
    var mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];
    function compareNumbers(a, b)
      return a - b;
    // again, assumes a print function is defined
    print("stringArray: " + stringArray.join() +"\n");
    print("Sorted: " + stringArray.sort() +"\n\n");
    print("numberArray: " + numberArray.join() +"\n");
    print("Sorted without a compare function: " + numberArray.sort() +"\n");
    print("Sorted with compareNumbers: " + numberArray.sort(compareNumbers) +"\n\n");
    print("numericStringArray: " + numericStringArray.join() +"\n");
    print("Sorted without a compare function: " + numericStringArray.sort() +"\n");
    print("Sorted with compareNumbers: " + numericStringArray.sort(compareNumbers) +"\n\n");
    print("mixedNumericArray: " + mixedNumericArray.join() +"\n");
    print("Sorted without a compare function: " + mixedNumericArray.sort() +"\n");
    print("Sorted with compareNumbers: " + mixedNumericArray.sort(compareNumbers) +"\n\n");

    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,Beluga
    Sorted: Beluga,Blue,Humpback
    numberArray: 40,1,5,200
    Sorted without a compare function: 1,200,40,5
    Sorted with compareNumbers: 1,5,40,200
    numericStringArray: 80,9,700
    Sorted without a compare function: 700,80,9
    Sorted with compareNumbers: 9,80,700
    mixedNumericArray: 80,9,700,40,1,5,200
    Sorted without a compare function: 1,200,40,5,700,80,9
    Sorted with compareNumbers: 1,5,9,40,80,200,700

    See Also

    join, reverse



    此页面的贡献者有: ziyunfei, Oatn, teoli
    最后编辑者: ziyunfei,