Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Javascript array sort asynchronous? [closed]

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
});

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

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.

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).

like image 670
Joe Mabel Avatar asked Jun 17 '14 17:06

Joe Mabel


1 Answers

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

The algorithm is explicitly specified here:

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

Get the this value.

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

Get the .length value.

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).

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.

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.

like image 105
Benjamin Gruenbaum Avatar answered Oct 22 '22 12:10

Benjamin Gruenbaum