Get dictionary rank by value in js

How do I generate a ranking for the value of kills in my dictionary?

I've tried everything about my poorly developed knowledge

for(let player in rank)
{
    dict.push({
        name: rank[player]["name"],
        rank: rank[player]["rank"],
            kills: rank[player]["kills"]
    });
}

This gets:

{'0': {name: 'Paul', rank: 2, kills: 12}}

{'1': {name: 'Mike', rank: 1, kills: 32}}

And I want to get

{'1': {name: 'Mike', rank: 1, kills: 32}}

{'0': {name: 'Paul', rank: 2, kills: 12}} <- Order by kill or rank result

1 answer

  • answered 2019-08-18 22:36 Mohamed Saeed

    Just use sort as following

    let dict = [
        {name: 'Paul', rank: 2, kills: 12},
        {name: 'Mike', rank: 1, kills: 32}
    ];
    
    let sortedByRankDict = dict.sort((a, b) => a.rank - b.rank);