0

I have the following object:

Object {by: "abritishguy", descendants: 38, id: 12617659, kids: Array[8], score: 133…}
Object {by: "mcgwiz", descendants: 4, id: 12641662, kids: Array[1], score: 8…}
Object {by: "dnetesn", descendants: 19, id: 12617263, kids: Array[5], score: 73…}
Object {by: "samber", descendants: 12, id: 12631162, kids: Array[6], score: 127…}
Object {by: "seanseanme", descendants: 0, id: 12632377, score: 16, time: 1475539307…}
Object {by: "wolframio", descendants: 3, id: 12631876, kids: Array[3], score: 25…}
Object {by: "paloaltokid", descendants: 22, id: 12620209, kids: Array[19], score: 11…}
Object {by: "vfulco", descendants: 1, id: 12620860, kids: Array[2], score: 7…}
Object {by: "forgingahead", descendants: 0, id: 12641351, score: 8, time: 1475638119…}
Object {by: "aylmao", descendants: 0, id: 12630489, score: 4, time: 1475522605…}

I need to sort the above object by "score"

So far I tried this:

$.getJSON('data.json', function (idata) {
    itemDataArray.push(idata);

});


$(itemDataArray).sort( function ( a, b ) {
    var attr = {}; // your comparison attribute here
    attr.a = parseInt( a.data( 'score' ) || 0 );
    attr.b = parseInt( b.data( 'score' ) || 0 );
    return attr.a < attr.b ? -1 : attr.a > attr.b ? 1 : 0;
});

it doesn't work, please help

Malarivtan
  • 404
  • 1
  • 6
  • 20
  • Don't wrap `itemDataArray ` in a jQuery object. Use native `sort`. – Karl-André Gagnon Oct 05 '16 at 13:07
  • you can also use lodash, it is really helpfull in a project https://lodash.com/docs/4.16.2#orderBy – Steeve Pitis Oct 05 '16 at 13:08
  • jQuery is mostly for DOM operations. Just use the native language unless you actually are building a jQuery object of elements. However, that `.sort` will happen before the `$.getJSON` response arrives. –  Oct 05 '16 at 13:08
  • ...since you're using `.data()`, it makes me think you've left out some important details. Seems pretty clear that you know how to use `.sort()`, whether on a jQuery object or a native Array. –  Oct 05 '16 at 13:12
  • ...I don't think that duplicate considers the details in the question, but since the OP isn't being responsive, I guess it just doesn't matter. –  Oct 05 '16 at 13:18

1 Answers1

1

You could use Array#sort directly.

In this case a possible string value is implicit casted to number, because of the - operator.

Ascending:

itemDataArray.sort(function (a, b) {
    return a.score - b.score;
});

Descending:

itemDataArray.sort(function (a, b) {
    return b.score - a.score;
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • it works the sorting part. when I parse using $.each it doesnt work. do you know why? – Malarivtan Oct 05 '16 at 13:22
  • @TanvirAlam, so ad hoc not. please ask a new question. – Nina Scholz Oct 05 '16 at 13:29
  • @TanvirAlam: Don't ask a new question unless you're willing to provide a complete example. This question should have never been answered in the first place. There are too many open questions about the full situation. This simple, obvious answer doesn't come close to covering the issues and possibilities presented in the question. –  Oct 05 '16 at 13:58