1

I have an example JSON file consisting of:

{
    "119996306407030785": {
        "duel": {
            "class": "mage",
            "totalDamage": 64,
            "wins": 5,
            "losses": 2
        },
        "weather": {
            "location": "46544"
        }
    },
    "119333755579138048": {
        "duel": {
            "class": "rogue",
            "totalDamage": 35,
            "losses": 1,
            "wins": 2
        },
        "weather": {
            "location": "95825"
        }
    },
    "112006834713329664": {
        "duel": {
            "totalDamage": 33,
            "losses": 1,
            "wins": 7
        }
    }
}

119996306407030785 is a userID. I'd like to sort the return by the highest duel.wins of all users for a "top 5" concept.

The output I would like with the above JSON would be:

112006834713329664 - 7 wins   
119996306407030785 - 5 wins  
119333755579138048 - 2 wins 

I've seen multiple questions close to this one while Googling, but can't find any that have JSON similar to my setup.

Is this possible considering how my userID's are setup & how random they are? If so, how can it be done? Please take note that I would not only need the return of the # of wins, but also the userID correlating to that win amount. I would also like to only return the top 5 even if there are dozens of userID's.

Adnan Umer
  • 3,669
  • 2
  • 18
  • 38
Log1x
  • 35
  • 1
  • 8
  • 1
    Possible duplicate of [Sort JavaScript object by key](http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Adnan Umer Mar 29 '16 at 17:32
  • This has already been answered here: http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – Akshat Mahajan Mar 29 '16 at 17:32
  • http://stackoverflow.com/questions/17684921/sort-json-object-in-javascript – Abhishek Pachal Mar 29 '16 at 17:33
  • what is with same wins, does them count as one? – Nina Scholz Mar 29 '16 at 17:33
  • Same wins would just sort accordingly. I could possibly add a conditional to factor losses into who would actually be #1 if wins are the same. As far as the "possibly dupes" above-- their JSON doesn't seem to look like mine as well as the answers don't explain how to turn the entire object into an array if that's what needs to be done to sort. I'd like a better example with my data if possible as I have seen those answers before posting this, but was unable to implement it into my needs. – Log1x Mar 29 '16 at 17:35
  • @Log1x [Matt's](http://stackoverflow.com/a/5467142/1169519) answer actually contains all the information you need. – Teemu Mar 29 '16 at 17:42
  • The concept of sorting a javascript object doesn't exist, because no sort order can be guaranteed on the object's properties. What you want to do is put that info into an array and sort that. – James Mar 29 '16 at 17:44

3 Answers3

0

var data = { "119996306407030785": { "duel": { "class": "mage", "totalDamage": 64, "wins": 5, "losses": 2 }, "weather": { "location": "46544" } }, "119333755579138048": { "duel": { "class": "rogue", "totalDamage": 35, "losses": 1, "wins": 2 }, "weather": { "location": "95825" } }, "112006834713329664": { "duel": { "totalDamage": 33, "losses": 1, "wins": 7 } } } 

var a = Object.keys(data).map(e => ({id: e, wins: data[e].duel.wins}))
        .sort((a, b) => b.wins - a.wins);

document.write('<pre>' + JSON.stringify(a, 0, 2) + '</pre>')
isvforall
  • 8,768
  • 6
  • 35
  • 50
0

To sort objects by duel.wins try this

function sortByWins(obj) {
    return Object.keys(obj).sort(function(i, j) {
        return obj[i].duel.wins - obj[j].duel.wins;
    }).reduce(function (result, key) {
        result[key] = obj[key];
        return result;
    }, {});
}
Adnan Umer
  • 3,669
  • 2
  • 18
  • 38
0

Loop over the object using the keys:

Object.keys(obj).reduce(function (p, c) {

  // return an array of winner objects
  return p.concat({ id: c, wins: obj[c].duel.wins });
}, []).sort(function (a, b) {

  // sort the objects by wins
  return a.wins < b.wins;
}).forEach(function (el) {

  // Display the information
  console.log([el.id,'-',el.wins,'wins'].join(' '));
});
Andy
  • 61,948
  • 13
  • 68
  • 95