7

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

19
  • 12
    No. It is not asynchronous. Commented Jun 17, 2014 at 17:09
  • "I see the POST from my saveAll in the console before the DEBUG 2 above" I don't understand what saveAll() has to do with DEBUG 1. What do you mean by "I see the POST..."? Commented Jun 17, 2014 at 17:13
  • Phrogz: as I say, that was always my understanding, but can you see any way to account for what I'm seeing in Firebug? Or the fact that it isn't writing back the changed table order? Commented Jun 17, 2014 at 17:13
  • Cookie monster: Each of those writes to the console, and can be viewed in the console pane of Firebug. (POSTs show up in the console.) Commented Jun 17, 2014 at 17:15
  • 1
    ...oh wait, your compare-function doens't return a value! With no return statement, there can be no sorting taking place. Did you mean to do return compareRowsBasedOnColumn(row1, row2, $this.data('sort-column'));? Commented Jun 17, 2014 at 17:27

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.