rankby is a tiny package that ranks arrays of objects in a decending manner. it handles ties even!
npm install rankby
in your code, use rankby like this:
let rankedObject = rankby(object, key);
const rankby = require('rankby');
let simple = [{ score: 1 }, { score: 2 }, { score: 4 }, { score: 5 }];
simple = rankby(simple, 'score');
console.log(simple);
=> [{ score: 5, rank: 1 }, { score: 4, rank: 2 }, { score: 2, rank: 3 }, { score: 1, rank: 4 }]
let ties = [{ score: 1 }, { score: 2 }, { score: 5 }, { score: 5 }];
ties = rankby(ties, 'score');
console.log(ties);
=> [{ score: 5, rank: 1 }, { score: 5, rank: 1 }, { score: 2, rank: 3 }, { score: 1, rank: 4 }]
MIT