-6
[
  [ 'UserA', 490 ],
  [ 'UserC', 175 ],
  [ 'UserD', 67 ],
  [ 'UserB', 26 ]
]

How would I give them a rank based on the leaderboard? eg. UserA is in the 1st Rank, UserC is in the 2nd Rank, etc.

It will look something like this:

UserA: Rank 1
UserC: Rank 2
UserD: Rank 3
UserB: Rank 4
Reinhardt
  • 9
  • 2
  • just use `arr.sort()` and them use `map` with `index + 1`. – DecPK Jul 12 '21 at 12:24
  • 1
    Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+sort+array+site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Jul 12 '21 at 12:25
  • 1
    This would be a more logical object: `{ 'UserA': 490, 'UserC': 175, 'UserD': 67 , 'UserB': 26 }` – mplungjan Jul 12 '21 at 12:26
  • 1
    Would be appreciated that you add something like: *"This is my code so far, I'm stuck at this ** and this** - I also searched this resources and tried this and this."*. Create a [mcve] of your best try. – Roko C. Buljan Jul 12 '21 at 12:27

1 Answers1

1

Use an object and sort it using a sort function

const results = {
  'UserA': { points: 490 },
  'UserB': { points: 26  },
  'UserC': { points: 175 },
  'UserD': { points: 67  }
}
const rank = Object.entries(results)
  .sort(([, a], [, b]) => b.points - a.points)
  .map((item, i) => {
    results[item[0]].rank = (i + 1)
    console.log(item[0], results[item[0]].rank)
    return `${item[0]}'s Rank: ${i+1}`
  })
console.log(results)

const findUserRank = user => rank.filter(entry => entry.includes(user))
const getUserRankOnly = user => results[user].rank
const getRankUser = ranking => Object.entries(results).filter(([key,val]) => val.rank===ranking).map(([key]) => key)


console.log("Users containing 'User':", findUserRank('User').join("\n"))
console.log("User with rank #3", getRankUser(3)[0])
console.log("UserC's rank:", getUserRankOnly('UserC'))

Same with your nested array

const results = [
  [ 'UserA', 490 ],
  [ 'UserC', 175 ],
  [ 'UserD', 67 ],
  [ 'UserB', 26 ]
];
const rank = results.slice(0)
  .sort(([, a], [, b]) => b - a)
  .map((item,i) => `${item[0]}'s Rank: ${i+1}`);

const getUserRank     = user => rank.filter(entry => entry.includes(user))
const getUserRankOnly = user => rank.filter(entry => entry.includes(user))[0].split(": ")[1]
const getRankUser     = ranking => rank.filter(entry => entry.endsWith(ranking))[0].split("'")[0]


console.log("Users containing 'User':",getUserRank('User').join("\n"))
console.log("Users containing 'UserC':",getUserRank('UserC')[0])
console.log("User with rank #3",getRankUser('3'))
console.log("UserC's rank:",getUserRankOnly('UserC')[0])
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Not exactly as I wanted it to be. So I'm having trouble figuring out how to display them as rank in numbers (I did already sort them). Again, it'll look like this as an example: ``` UserA's Rank: 1 UserB's Rank: 4 etc ``` – Reinhardt Jul 12 '21 at 12:43
  • Oh yeah, also I forgot one thing. How do I only get the specific rank from the username? eg. I wanted to fetch for **UserA** and it'll display the rank number. – Reinhardt Jul 12 '21 at 13:48
  • Did you see the update @ReinhardtG ? – mplungjan Jul 12 '21 at 20:15