How to sort scores for high score board in a gameHow do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow do I check if an array includes an object in JavaScript?How do I sort a dictionary by value?Sort array of objects by string property valueHow to sort a dataframe by multiple column(s)Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missingWhy is processing a sorted array faster than processing an unsorted array?How to pair socks from a pile efficiently?What is the optimal algorithm for the game 2048?
Is this broken pipe the reason my freezer is not working? Can it be fixed?
How can the US president give an order to a civilian?
How is linear momentum conserved in circular motion?
How to ask if I can mow my neighbor's lawn
How can caller ID be faked?
Does cooling a potato change the nature of its carbohydrates?
I just entered the USA without passport control at Atlanta airport
Scaling an object to change its key
Explicit song lyrics checker
Are there examples of rowers who also fought?
how to find which software is doing ssh connection?
Why is it 出差去 and not 去出差?
My student in one course asks for paid tutoring in another course. Appropriate?
Is swap gate equivalent to just exchanging the wire of the two qubits?
What is this plant I saw for sale at a Romanian farmer's market?
How did Frodo know where the Bree village was?
Can a character with the Polearm Master feat make an opportunity attack against an invisible creature that enters their reach?
Are there any individual aliens that have gained superpowers in the Marvel universe?
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?
How do I become a better writer when I hate reading?
Fibonacci sequence and other metallic sequences emerged in the form of fractions
Time travel short story where someone from the past follows the travelers back
Got a new frameset, don't know why I need this split ring collar?
How to sort scores for high score board in a game
How do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow do I check if an array includes an object in JavaScript?How do I sort a dictionary by value?Sort array of objects by string property valueHow to sort a dataframe by multiple column(s)Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missingWhy is processing a sorted array faster than processing an unsorted array?How to pair socks from a pile efficiently?What is the optimal algorithm for the game 2048?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm writing a game in which you can have different users. Every time you play the game you submit a score. I want to have a high-score board that show the top 10 scores of all time. For instance:
A:[100,200,50]
B:[400,150,320]
C:[50,245,35]
What I want to achieve is:
B 400
B 320
C 245
A 200
B 150
A 100
A 50
C 50
C 35
Sorting the numbers themselves isn't a big problem but how can I keep track of what number is for which player? I'm using Java and I figured maybe a HashMap would be useful but I couldn't find a solution.
Basically:
- I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?
- What is the fastest algorithm I can use to do this task?
- If two scores are the same the alphabetical order of player names should be checked, how can I do this?
java algorithm sorting
add a comment |
I'm writing a game in which you can have different users. Every time you play the game you submit a score. I want to have a high-score board that show the top 10 scores of all time. For instance:
A:[100,200,50]
B:[400,150,320]
C:[50,245,35]
What I want to achieve is:
B 400
B 320
C 245
A 200
B 150
A 100
A 50
C 50
C 35
Sorting the numbers themselves isn't a big problem but how can I keep track of what number is for which player? I'm using Java and I figured maybe a HashMap would be useful but I couldn't find a solution.
Basically:
- I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?
- What is the fastest algorithm I can use to do this task?
- If two scores are the same the alphabetical order of player names should be checked, how can I do this?
java algorithm sorting
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33
add a comment |
I'm writing a game in which you can have different users. Every time you play the game you submit a score. I want to have a high-score board that show the top 10 scores of all time. For instance:
A:[100,200,50]
B:[400,150,320]
C:[50,245,35]
What I want to achieve is:
B 400
B 320
C 245
A 200
B 150
A 100
A 50
C 50
C 35
Sorting the numbers themselves isn't a big problem but how can I keep track of what number is for which player? I'm using Java and I figured maybe a HashMap would be useful but I couldn't find a solution.
Basically:
- I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?
- What is the fastest algorithm I can use to do this task?
- If two scores are the same the alphabetical order of player names should be checked, how can I do this?
java algorithm sorting
I'm writing a game in which you can have different users. Every time you play the game you submit a score. I want to have a high-score board that show the top 10 scores of all time. For instance:
A:[100,200,50]
B:[400,150,320]
C:[50,245,35]
What I want to achieve is:
B 400
B 320
C 245
A 200
B 150
A 100
A 50
C 50
C 35
Sorting the numbers themselves isn't a big problem but how can I keep track of what number is for which player? I'm using Java and I figured maybe a HashMap would be useful but I couldn't find a solution.
Basically:
- I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?
- What is the fastest algorithm I can use to do this task?
- If two scores are the same the alphabetical order of player names should be checked, how can I do this?
java algorithm sorting
java algorithm sorting
edited Mar 25 at 5:35
Sarthak Kumar
32
32
asked Mar 25 at 5:12
FarzinNasiriFarzinNasiri
9417
9417
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33
add a comment |
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33
add a comment |
1 Answer
1
active
oldest
votes
You could write a class for a score and implement Comparable
.
class Score implements Comparable<Score>
int score;
Player player;
Score(int score, Player player)
this.score = score;
this.player = player;
@Override
public int compareTo(Score otherScore)
if (this.score > otherScore.score)
return 1;
else if (this.score < otherScore.score)
return -1;
else
return this.player.name.compareTo(otherScore.player.name);
This way you can just save the scores in a List
and use Collections.sort()
.
(in the above example, I assume you have a class for Player
with attribute name
)
1
Why not use the string'scompareTo()
method? This should just be 3 lines.
– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331575%2fhow-to-sort-scores-for-high-score-board-in-a-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could write a class for a score and implement Comparable
.
class Score implements Comparable<Score>
int score;
Player player;
Score(int score, Player player)
this.score = score;
this.player = player;
@Override
public int compareTo(Score otherScore)
if (this.score > otherScore.score)
return 1;
else if (this.score < otherScore.score)
return -1;
else
return this.player.name.compareTo(otherScore.player.name);
This way you can just save the scores in a List
and use Collections.sort()
.
(in the above example, I assume you have a class for Player
with attribute name
)
1
Why not use the string'scompareTo()
method? This should just be 3 lines.
– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
add a comment |
You could write a class for a score and implement Comparable
.
class Score implements Comparable<Score>
int score;
Player player;
Score(int score, Player player)
this.score = score;
this.player = player;
@Override
public int compareTo(Score otherScore)
if (this.score > otherScore.score)
return 1;
else if (this.score < otherScore.score)
return -1;
else
return this.player.name.compareTo(otherScore.player.name);
This way you can just save the scores in a List
and use Collections.sort()
.
(in the above example, I assume you have a class for Player
with attribute name
)
1
Why not use the string'scompareTo()
method? This should just be 3 lines.
– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
add a comment |
You could write a class for a score and implement Comparable
.
class Score implements Comparable<Score>
int score;
Player player;
Score(int score, Player player)
this.score = score;
this.player = player;
@Override
public int compareTo(Score otherScore)
if (this.score > otherScore.score)
return 1;
else if (this.score < otherScore.score)
return -1;
else
return this.player.name.compareTo(otherScore.player.name);
This way you can just save the scores in a List
and use Collections.sort()
.
(in the above example, I assume you have a class for Player
with attribute name
)
You could write a class for a score and implement Comparable
.
class Score implements Comparable<Score>
int score;
Player player;
Score(int score, Player player)
this.score = score;
this.player = player;
@Override
public int compareTo(Score otherScore)
if (this.score > otherScore.score)
return 1;
else if (this.score < otherScore.score)
return -1;
else
return this.player.name.compareTo(otherScore.player.name);
This way you can just save the scores in a List
and use Collections.sort()
.
(in the above example, I assume you have a class for Player
with attribute name
)
edited Mar 25 at 9:38
answered Mar 25 at 6:18
MWBMWB
1,17011020
1,17011020
1
Why not use the string'scompareTo()
method? This should just be 3 lines.
– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
add a comment |
1
Why not use the string'scompareTo()
method? This should just be 3 lines.
– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
1
1
Why not use the string's
compareTo()
method? This should just be 3 lines.– vivek_23
Mar 25 at 8:35
Why not use the string's
compareTo()
method? This should just be 3 lines.– vivek_23
Mar 25 at 8:35
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
Because I was not thinking right ;)
– MWB
Mar 25 at 9:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331575%2fhow-to-sort-scores-for-high-score-board-in-a-game%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Too many questions and too broad. Focus on one of your questions, add some code to the question and explain what your issue is with the code in more detail.
– Joakim Danielson
Mar 25 at 5:47
I voted to close as it's too broad. Also, sorting based on alphabetical names if scores are same isn't a fair way to judge their ranking.
– vivek_23
Mar 25 at 8:33