6

I have object {"5":"5","4":"4","3":"3","2":"2","1":"1","-1":"P1",-2":"P2"} And use this function to parse elements:

function floormake(inobj) {
    var levels = '';
    var obj = JSON.parse(inobj);
    levels += '<ul>';
    Object.keys(obj).sort(-1).forEach(function (key) {
        levels += '<li>' + obj[key] + '</li>';
    });
    levels += '</ul>';
    return levels;
}

But result alway sorting by number: -1, -2, 1, 2 etc. BUT i need reverse sorting: 5, 4, 3, 2, 1, sort(-1) - doesn't work

skywind
  • 892
  • 6
  • 22
  • 44
  • What you have wrote is not actually valid JSON. JSON keys cannot start with a number. http://stackoverflow.com/questions/8758715/using-number-as-index-json – Forbesmyester Apr 08 '13 at 12:45

2 Answers2

12

Consider using .reverse() instead.

Object.keys(obj).sort().reverse().forEach( ....

Reverse documentation

Edit Note: As mentioned by @Shmiddty, the reverse() method does not actually sort. The array will need to be sorted then reversed.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 2
    reverse doesn't sort the array. It simply reverses it. He probably wants `.sort().reverse()` – Shmiddty Nov 14 '12 at 15:31
  • @Shmiddty: That's very true. Thanks. I'll post an update to disambiguate. – Joel Etherton Nov 14 '12 at 15:37
  • 1
    `.sort()` does not sort numerically, though. It happens to work with one-digit numbers and a possible minus. – pimvdb Nov 14 '12 at 15:53
  • @pimvdb: `.sort()` sorts both alphabetically and numerically. In this case, however, OP indicated that the sort method was already functioning properly, he simply needed it reversed. If the problem had been with the sort, I would have recommended a different sorting algorithm entirely. – Joel Etherton Nov 14 '12 at 16:04
  • @JoelEtherton `.sort` does not sort numerically...where are you finding this? – Ian Nov 14 '12 at 16:12
  • @Ian: It's done through closures. It just isn't out of the box. – Joel Etherton Nov 14 '12 at 16:16
  • @JoelEtherton The point that simply calling `.sort()` does not sort numerically and that it requires the optional parameter to do so. – Ian Nov 14 '12 at 16:19
  • @Ian: A point tangent to the question at hand. "How do you reverse an already sorted array?" – Joel Etherton Nov 14 '12 at 16:20
  • @JoelEtherton The tangent point is that nowhere does it say it's already sorted. And the fact that it should be known that using `.sort()` or `.sort(-1)` would sort the numbers (which are already strings) in lexographical way, means that you shouldn't encourage incorrect code. If anything was tested with numbers outside of the OP's example, it would be found that `.sort()` itself doesn't work in the first place. – Ian Nov 14 '12 at 16:23
5

The Array.sort method does not accept an integer as the only optional parameter. It accepts a reference to a function that either returns -1, 0, or 1, and the details can be found here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

Here's an example of how to sort based on number:

http://jsfiddle.net/3HFN5/1/

var a = ["5", "9", "1", "399", "23", "21"];

function sorter(b, c) {
    return (+b < +c ? 1 : -1);
}

alert(a.sort(sorter));

Or something simpler/better:

http://jsfiddle.net/3HFN5/2/

var a = ["5", "9", "1", "399", "23", "21"];

function sorter(b, c) {
    return c - b;
}

alert(a.sort(sorter));

And incorporating this with your actual example:

http://jsfiddle.net/3HFN5/3/

var a = {"2":"2","4":"4","-2":"P2","3":"3","300":"4","1":"1","5":"5","-1":"P1"};

function sorter(b, c) {
    return c - b;
}

alert(Object.keys(a).sort(sorter));

I mixed the items in the object around and added one to prove it's sorting accurately/completely.

Ian
  • 50,146
  • 13
  • 101
  • 111