Creating a leaderboard in Java using primitive arrays [duplicate]










2
















This question already has an answer here:



  • Sort a parallel array using Arrays.sort()

    5 answers



I'm creating a console game in Java. And I want to keep track of the scores and names.



I've already created two arrays.



String PlayerNames = "Bob", "Rick", "Jack"; // just an example
int PlayerScores = 40, 20, 60; // just an example


I want to sort their scores, but also know who the score belongs to, and then print it out like this:



 Jack 60
Bob 40
Rick 20









share|improve this question















marked as duplicate by 4castle, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 16 '18 at 14:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

    – Samuel
    Nov 15 '18 at 19:29
















2
















This question already has an answer here:



  • Sort a parallel array using Arrays.sort()

    5 answers



I'm creating a console game in Java. And I want to keep track of the scores and names.



I've already created two arrays.



String PlayerNames = "Bob", "Rick", "Jack"; // just an example
int PlayerScores = 40, 20, 60; // just an example


I want to sort their scores, but also know who the score belongs to, and then print it out like this:



 Jack 60
Bob 40
Rick 20









share|improve this question















marked as duplicate by 4castle, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 16 '18 at 14:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

    – Samuel
    Nov 15 '18 at 19:29














2












2








2


1







This question already has an answer here:



  • Sort a parallel array using Arrays.sort()

    5 answers



I'm creating a console game in Java. And I want to keep track of the scores and names.



I've already created two arrays.



String PlayerNames = "Bob", "Rick", "Jack"; // just an example
int PlayerScores = 40, 20, 60; // just an example


I want to sort their scores, but also know who the score belongs to, and then print it out like this:



 Jack 60
Bob 40
Rick 20









share|improve this question

















This question already has an answer here:



  • Sort a parallel array using Arrays.sort()

    5 answers



I'm creating a console game in Java. And I want to keep track of the scores and names.



I've already created two arrays.



String PlayerNames = "Bob", "Rick", "Jack"; // just an example
int PlayerScores = 40, 20, 60; // just an example


I want to sort their scores, but also know who the score belongs to, and then print it out like this:



 Jack 60
Bob 40
Rick 20




This question already has an answer here:



  • Sort a parallel array using Arrays.sort()

    5 answers







java arrays sorting console leaderboard






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:01









gfos

69121021




69121021










asked Nov 15 '18 at 19:24









Lukas Méndez DuusLukas Méndez Duus

255




255




marked as duplicate by 4castle, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 16 '18 at 14:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by 4castle, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 16 '18 at 14:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

    – Samuel
    Nov 15 '18 at 19:29













  • 1





    FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

    – Samuel
    Nov 15 '18 at 19:29








1




1





FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

– Samuel
Nov 15 '18 at 19:29






FYI, map could be your friend here. Also, what do you base on to sort the users (PlayerNames)?

– Samuel
Nov 15 '18 at 19:29













2 Answers
2






active

oldest

votes


















1














Create a map with the player names as the keys and the scores as the values, then sort the map based on the values:



public static void main(String args) 
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
unsortedMap.put("Jack", 60);
unsortedMap.put("Bob", 40);
unsortedMap.put("Rick", 20);

Map<String, Integer> sortedMap = sortByValue(unsortedMap);
printMap(sortedMap);


private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap)

// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
return (o1.getValue()).compareTo(o2.getValue());

);

// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list)
sortedMap.put(entry.getKey(), entry.getValue());


/*
//classic iterator example
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); )
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
*/


return sortedMap;


public static <K, V> void printMap(Map<K, V> map)
for (Map.Entry<K, V> entry : map.entrySet())
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());




Note: see https://www.mkyong.com/java/how-to-sort-a-map-in-java/ for more details.






share|improve this answer




















  • 1





    Used this method, and works perfectly! Thank you very much!

    – Lukas Méndez Duus
    Nov 16 '18 at 7:36


















0














You can use a hashmap for this. Use each name from playerNames as a key and create a list for the values (in case two or more players with the same name get a score). A hashmap allows only one value per key, which is why you should create a list of integers for the scores.



Map<String, List<Integer>> scoreboard = new HashMap<>();





share|improve this answer

























  • You will never have more than one player with the same name. Maps don't allow duplicate keys.

    – gfos
    Nov 15 '18 at 21:50

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Create a map with the player names as the keys and the scores as the values, then sort the map based on the values:



public static void main(String args) 
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
unsortedMap.put("Jack", 60);
unsortedMap.put("Bob", 40);
unsortedMap.put("Rick", 20);

Map<String, Integer> sortedMap = sortByValue(unsortedMap);
printMap(sortedMap);


private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap)

// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
return (o1.getValue()).compareTo(o2.getValue());

);

// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list)
sortedMap.put(entry.getKey(), entry.getValue());


/*
//classic iterator example
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); )
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
*/


return sortedMap;


public static <K, V> void printMap(Map<K, V> map)
for (Map.Entry<K, V> entry : map.entrySet())
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());




Note: see https://www.mkyong.com/java/how-to-sort-a-map-in-java/ for more details.






share|improve this answer




















  • 1





    Used this method, and works perfectly! Thank you very much!

    – Lukas Méndez Duus
    Nov 16 '18 at 7:36















1














Create a map with the player names as the keys and the scores as the values, then sort the map based on the values:



public static void main(String args) 
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
unsortedMap.put("Jack", 60);
unsortedMap.put("Bob", 40);
unsortedMap.put("Rick", 20);

Map<String, Integer> sortedMap = sortByValue(unsortedMap);
printMap(sortedMap);


private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap)

// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
return (o1.getValue()).compareTo(o2.getValue());

);

// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list)
sortedMap.put(entry.getKey(), entry.getValue());


/*
//classic iterator example
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); )
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
*/


return sortedMap;


public static <K, V> void printMap(Map<K, V> map)
for (Map.Entry<K, V> entry : map.entrySet())
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());




Note: see https://www.mkyong.com/java/how-to-sort-a-map-in-java/ for more details.






share|improve this answer




















  • 1





    Used this method, and works perfectly! Thank you very much!

    – Lukas Méndez Duus
    Nov 16 '18 at 7:36













1












1








1







Create a map with the player names as the keys and the scores as the values, then sort the map based on the values:



public static void main(String args) 
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
unsortedMap.put("Jack", 60);
unsortedMap.put("Bob", 40);
unsortedMap.put("Rick", 20);

Map<String, Integer> sortedMap = sortByValue(unsortedMap);
printMap(sortedMap);


private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap)

// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
return (o1.getValue()).compareTo(o2.getValue());

);

// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list)
sortedMap.put(entry.getKey(), entry.getValue());


/*
//classic iterator example
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); )
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
*/


return sortedMap;


public static <K, V> void printMap(Map<K, V> map)
for (Map.Entry<K, V> entry : map.entrySet())
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());




Note: see https://www.mkyong.com/java/how-to-sort-a-map-in-java/ for more details.






share|improve this answer















Create a map with the player names as the keys and the scores as the values, then sort the map based on the values:



public static void main(String args) 
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
unsortedMap.put("Jack", 60);
unsortedMap.put("Bob", 40);
unsortedMap.put("Rick", 20);

Map<String, Integer> sortedMap = sortByValue(unsortedMap);
printMap(sortedMap);


private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap)

// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
return (o1.getValue()).compareTo(o2.getValue());

);

// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list)
sortedMap.put(entry.getKey(), entry.getValue());


/*
//classic iterator example
for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); )
Map.Entry<String, Integer> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
*/


return sortedMap;


public static <K, V> void printMap(Map<K, V> map)
for (Map.Entry<K, V> entry : map.entrySet())
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());




Note: see https://www.mkyong.com/java/how-to-sort-a-map-in-java/ for more details.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 1 at 21:52

























answered Nov 15 '18 at 21:21









gfosgfos

69121021




69121021







  • 1





    Used this method, and works perfectly! Thank you very much!

    – Lukas Méndez Duus
    Nov 16 '18 at 7:36












  • 1





    Used this method, and works perfectly! Thank you very much!

    – Lukas Méndez Duus
    Nov 16 '18 at 7:36







1




1





Used this method, and works perfectly! Thank you very much!

– Lukas Méndez Duus
Nov 16 '18 at 7:36





Used this method, and works perfectly! Thank you very much!

– Lukas Méndez Duus
Nov 16 '18 at 7:36













0














You can use a hashmap for this. Use each name from playerNames as a key and create a list for the values (in case two or more players with the same name get a score). A hashmap allows only one value per key, which is why you should create a list of integers for the scores.



Map<String, List<Integer>> scoreboard = new HashMap<>();





share|improve this answer

























  • You will never have more than one player with the same name. Maps don't allow duplicate keys.

    – gfos
    Nov 15 '18 at 21:50















0














You can use a hashmap for this. Use each name from playerNames as a key and create a list for the values (in case two or more players with the same name get a score). A hashmap allows only one value per key, which is why you should create a list of integers for the scores.



Map<String, List<Integer>> scoreboard = new HashMap<>();





share|improve this answer

























  • You will never have more than one player with the same name. Maps don't allow duplicate keys.

    – gfos
    Nov 15 '18 at 21:50













0












0








0







You can use a hashmap for this. Use each name from playerNames as a key and create a list for the values (in case two or more players with the same name get a score). A hashmap allows only one value per key, which is why you should create a list of integers for the scores.



Map<String, List<Integer>> scoreboard = new HashMap<>();





share|improve this answer















You can use a hashmap for this. Use each name from playerNames as a key and create a list for the values (in case two or more players with the same name get a score). A hashmap allows only one value per key, which is why you should create a list of integers for the scores.



Map<String, List<Integer>> scoreboard = new HashMap<>();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 21:56









Alperen

1,3961721




1,3961721










answered Nov 15 '18 at 21:43









NullPointerNullPointer

86




86












  • You will never have more than one player with the same name. Maps don't allow duplicate keys.

    – gfos
    Nov 15 '18 at 21:50

















  • You will never have more than one player with the same name. Maps don't allow duplicate keys.

    – gfos
    Nov 15 '18 at 21:50
















You will never have more than one player with the same name. Maps don't allow duplicate keys.

– gfos
Nov 15 '18 at 21:50





You will never have more than one player with the same name. Maps don't allow duplicate keys.

– gfos
Nov 15 '18 at 21:50



這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto