Jump to content
Sign in to follow this  
voodoodan

Arrays - Leaderboard help requested

Recommended Posts

Hello,

 

I'm trying to make some kind of leaderboard, such as the example below:

 

var game = ["Dominoes",95,"Cards",22,"Chess",65];
The idea is to try to group all of the numbers together in the array so that I can return a position on them.
For example:
95 - would return 1st place (for the most points)
65 - 2nd place
22 - 3rd place
But the numbers in the "game" variable would also need to be linked in with the games so that if they were sorted numerically then the games would follow suit as well.
Would anyone be able to help with this, please? I'd be grateful for any assistance.
Many thanks,Dan.

Share this post


Link to post
Share on other sites

A different data structure would be needed. Something like this, for example:

var game = [  { name: "Dominoes", score: 95 },  { name: "Cards", score: 22 },  { name: "Chess", score: 65 }];

Then you could use the sort() method and pass in a function that compares the scores of each item.

  • Like 1

Share this post


Link to post
Share on other sites

A different data structure would be needed. Something like this, for example:

var game = [  { name: "Dominoes", score: 95 },  { name: "Cards", score: 22 },  { name: "Chess", score: 65 }];

Then you could use the sort() method and pass in a function that compares the scores of each item.

 

Thanks very much for this Ingolme, I'll give that structure a go.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×