Skip to main content
November 12, 2008
Question

array sort in descending order doesn't work

  • November 12, 2008
  • 1 reply
  • 542 views
"Please use "Attach Code" to include any sample code in the message.". MMM. There's no attach code link.

The array.sort function doesn't sort in descending order, the output is the same.

Formatted: http://pastebin.com/f3a320622
//Ascending
var array = [{number:1}, {number:2}];
array.sort(function(a,b) {
if (a.number < b.number)
return -1;
else if (a.number > b.number)
return 1;
else
return 0;
});
array.trace();


Formatted: http://pastebin.com/m625c53df
//Descending
var array = [{number:1}, {number:2}];
array.sort(function(a,b) {
if (a.number < b.number)
return -1;
else if (a.number > b.number)
return 1;
else
return 0;
}, Array.DESCENDING);
array.trace();


Here's the additional functions (trace):
http://pastebin.com/f42937d95

Since they can't even fix the attach code thing, i have no hope they can fix this. But whatever.
    This topic has been closed for replies.

    1 reply

    November 12, 2008
    btw, this is what i had to do to fix it:
    Array.prototype.nativeSort = Array.prototype.sort;
    Array.prototype.sort = function(cmp, options)
    {
    this.nativeSort(cmp, options);
    if (options == Array.DESCENDING)
    this.reverse();
    }