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).
saveAll()has to do withDEBUG 1. What do you mean by "I see the POST..."?compare-functiondoens't return a value! With noreturnstatement, there can be no sorting taking place. Did you mean to doreturn compareRowsBasedOnColumn(row1, row2, $this.data('sort-column'));?