Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 二维阵列的默认排序是什么?_Javascript_Sorting - Fatal编程技术网

Javascript 二维阵列的默认排序是什么?

Javascript 二维阵列的默认排序是什么?,javascript,sorting,Javascript,Sorting,使用Array.sort()时 我假设它需要第一个索引,但我想验证一下 我什么也没说: 这是不相关的 对于这种方法:(对第一个索引进行排序就可以了) 从您链接到的文档: 数组根据每个元素的字符串转换按字典顺序进行排序 这不会因为这些元素也是数组而改变。让我们检查一下数组的EMCAScript规范。sort算法迭代比较数组中的元素对;这些比较产生-1、1或0(分别用于小于、大于或等于),并且每个比较的结果用于构建排序数组 我们特别关注排序的“默认”比较情况,其中没有指定比较函数。比较某对x和y时:

使用Array.sort()时

我假设它需要第一个索引,但我想验证一下

我什么也没说:

这是不相关的

对于这种方法:(对第一个索引进行排序就可以了)


从您链接到的文档:

数组根据每个元素的字符串转换按字典顺序进行排序


这不会因为这些元素也是数组而改变。

让我们检查一下数组的EMCAScript规范。
sort
算法迭代比较数组中的元素对;这些比较产生
-1
1
0
(分别用于小于、大于或等于),并且每个比较的结果用于构建排序数组

我们特别关注
排序
的“默认”比较情况,其中没有指定比较函数。比较某对
x
y
时:

  • 设xString为ToString(x)
  • 让我们把y串起来
  • 如果xString
  • 如果xString>yString,则返回1
  • 返回+0
ECMAScript应用于对象时,调用对象的
toString
方法

为了阐明
x>y
x
的含义,我们可以检查ECMAScript,它指定了

$P.aZindex = function () {
    var arr_2d = {},
        elements = document.body.getElementsByTagName("*"),
        element,
        length,
        z_index;

    // loop through elements and pull information from them
    $A.eachIndex(elements, function(val, index){
        z_index = win.getComputedStyle(val).getPropertyValue("z-index");

        // ignore elements with the auto value
        if (z_index !== "auto") {
            arr_2d[i] = [val.id, val.tagName, val.className, z_index];
        }
    });

    // sort the array
    arr_2d.sort();
    return arr_2d;
};
var compare_items = function(a,b) {
    var result;
    if(a instanceof Array && b instanceof Array) {
        // iteratively compare items from each array
        for(var i=0; i<Math.min(a.length,b.length); ++i) {
            result = compare_items(a[i], b[i]);
            if(result != 0) { return result; }
        }

        // if both arrays are equal so far, length is the determining factor
        return a.length - b.length;
    }

    // if the items are both numbers, report their numeric relation
    if(typeof a == "number" && typeof b == "number") {
        return a - b;
    }

    // otherwise, fall back to strings
    if(a.toString() == b.toString()) { return 0; }
    return a.toString() > b.toString() ? 1 : -1;
}