Javascript数组排序是异步的吗? [英] Is Javascript array sort asynchronous?

查看:104
本文介绍了Javascript数组排序是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript Array.sort函数是否异步?我不这么认为,但是当我运行以下代码时,它确实似乎是:

Is the Javascript Array.sort function asynchronous? I wouldn't think so, but when I run the following code, it sure seems to be:

$('#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,但它几乎就是名字所说的。)

(I can provide compareRowsBasedOnColumn if needed, but it's pretty much what the name says.)

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

Running in Firefox with the Firebug debugger, I see the POST from my saveAll in the console before the DEBUG 2 above, interspersed with the DEBUG 1s, and I don't get my content effectively resorted. DEBUG 1 is giving me the results I'd expect.

另外,只有Javascript数组才有意义。 sort函数是异步的。

Offhand, this makes sense only the Javascript Array.sort function is asynchronous.

如果它确实是异步的,那么任何人都可以建议一个改写这个的好方法,而不是写我自己的那种(我真的宁愿坚持他们的,如果只是为了清楚起见。

If, indeed, it is asynchronous, can anyone suggest a good way to rewrite this, short of writing my own sort (I'd really rather stick with theirs, if only for clarity).

推荐答案

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

Yes. Array#sort is guaranteed to be synchronous by the ECMAScript specification on which JavaScript is based on.

此处明确指定算法:


让obj成为调用ToObject传递此值作为参数的结果。

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

获取这个值。


设len是将Uint32应用于调用[[Get]的结果的结果]带有参数length的obj的内部方法。

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

获取 .length value。


如果comparefn未定义且不是此数组元素的一致比较函数(见下文),则行为sort是实现定义的。

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.

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

Get the passed comparison function. If it is undefined, the implementation may do whatever it wants (in practice, it does lexical sort, however it has to be sync since we wait for it as we'll soon see).


执行依赖于实现的调用序列,调用obj的[[Get]],[[Put]]和[[Delete]]内部方法以及SortCompare(如下所述) ),每次调用[[Get]],[[Put]]或[[Delete]]的第一个参数是一个小于len的非负整数,并且调用SortCompare的参数是先前调用的结果。 [[Get]]内部方法。 [[Put]]和[[Delete]]内部方法的throw参数将为true。如果obj不稀疏,则不能调用[[Delete]]。

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.

返回obj。

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

So, it performs the operations in SortCompare. Which just compares them (a few lines below).

请注意,使用的排序是实现定义的(实际上在实现中有所不同),也不能保证稳定。

Note that the sort used is implementation defined (and actually varies across implementation), it is also not guaranteed to be stable.

这篇关于Javascript数组排序是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆