javascript - Javascript 数组排序是异步的吗?

标签 javascript jquery sorting asynchronous

<分区>

Javascript Array.sort 函数是异步的吗?我不这么认为,但是当我运行以下代码时,它确实看起来是:

$('#alphabetical-order').data('sort-column', 'FileAlpha');
$('#first-numeric-order').data('sort-column', 'FileFirstNumeric');
$('#last-numeric-order').data('sort-column', 'FileLastNumeric');
$('#alphabetical-order, #first-numeric-order, #last-numeric-order').each(function() {
    var $this = $(this);
    $this.data('compare-function', function(row1, row2) {
        console.log('column = ' + $this.data('sort-column')); // >> DEBUG 1
        compareRowsBasedOnColumn(row1, row2, $this.data('sort-column'));
    });
}).click(function() {
    var $this = $(this);
    var $content = $('table.sheetlist-content tr.content');
    $content.sort($this.data('compare-function'));
    console.log('$content.sort complete'); // >> DEBUG 2
    $table_body = $('table.sheetlist-content tbody')
    $table_body.html('');

    for (i=0; i<$content.length; ++i) {
        $table_body.append($content[i]);
    }
    saveAll(); // which POSTs to our server
});

(如果需要,我可以提供 compareRowsBasedOnColumn,但正如其名称所表达的那样。)

使用 Firebug 调试器在 Firefox 中运行,我在控制台中看到 saveAll 的 POST 之前上面的 DEBUG 2,穿插着DEBUG 1,我没有有效地利用我的内容。 DEBUG 1 给出了我期望的结果。

顺便说一下,这只有 Javascript Array.sort 函数是异步的才有意义。

如果它确实是异步的,除了编写我自己的类型之外,任何人都可以建议一种重写它的好方法(如果只是为了清楚起见,我真的宁愿坚持使用他们的方法)。

最佳答案

是的。 Array#sort JavaScript 所基于的 ECMAScript 规范保证同步。

此处明确指定算法:

Let obj be the result of calling ToObject passing the this value as the argument.

获取这个值。

Let len be the result of applying Uint32 to the result of calling the [[Get]] internal method of obj with argument "length".

获取.length值。

If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the behaviour of sort is implementation-defined.

获取传递过来的比较函数。如果它未定义,则实现可以做任何它想做的事情(实际上,它会进行词法排序,但它必须是同步的,因为我们会等待它,我们很快就会看到)。

Perform an implementation-dependent sequence of calls to the [[Get]] , [[Put]], and [[Delete]] internal methods of obj and to SortCompare (described below), where the first argument for each call to [[Get]], [[Put]], or [[Delete]] is a nonnegative integer less than len and where the arguments for calls to SortCompare are results of previous calls to the [[Get]] internal method. The throw argument to the [[Put]] and [[Delete]] internal methods will be the value true. If obj is not sparse then [[Delete]] must not be called.

Return obj.

因此,它执行 SortCompare 中的操作。这只是比较它们(下面几行)。

请注意,使用的排序是实现定义的(并且实际上因实现而异),也不能保证稳定。

关于javascript - Javascript 数组排序是异步的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24269624/

相关文章:

algorithm - 什么是服从条件的好的排序算法?

javascript - Switch Case Javascript 中的双管

javascript - 为什么我的 SVN 预提交 Hook 可以在本地工作,但不能在提交时工作?

javascript - jstree 3.0.0 卡在加载 json 数据上

javascript - 当有更多提交按钮可用时处理多个 Angular 调用

javascript - 如何使用javascript按组对数组进行排序?

bash - 使用 Bash 的两个列表之间的区别

javascript - 处理 React 中的 localStorage 更改以重新渲染组件

javascript - LimeJS 圆形边框

javascript - 单个表单上的两个提交按钮