Skip to main content
Nov 12, 2008 10:58 AM UTC
Question

array sort in descending order doesn't work

"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 is closed to new replies. Start a new post to keep the conversation going.

    1 reply

    Nov 12, 2008 11:04 AM UTC
    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();
    }
    Translate