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;








1















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:



  1. I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?

  2. What is the fastest algorithm I can use to do this task?

  3. If two scores are the same the alphabetical order of player names should be checked, how can I do this?









share|improve this question
























  • 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

















1















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:



  1. I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?

  2. What is the fastest algorithm I can use to do this task?

  3. If two scores are the same the alphabetical order of player names should be checked, how can I do this?









share|improve this question
























  • 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













1












1








1








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:



  1. I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?

  2. What is the fastest algorithm I can use to do this task?

  3. If two scores are the same the alphabetical order of player names should be checked, how can I do this?









share|improve this question
















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:



  1. I already have the list of scores of each player. How should I save this data for better sorting? Is HashMap a good idea?

  2. What is the fastest algorithm I can use to do this task?

  3. If two scores are the same the alphabetical order of player names should be checked, how can I do this?






java algorithm sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















3














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)






share|improve this answer




















  • 1





    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











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
);



);













draft saved

draft discarded


















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









3














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)






share|improve this answer




















  • 1





    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















3














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)






share|improve this answer




















  • 1





    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













3












3








3







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)






share|improve this answer















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)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 9:38

























answered Mar 25 at 6:18









MWBMWB

1,17011020




1,17011020







  • 1





    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












  • 1





    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







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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현