diff --git a/src/main/java/uta/cse3310/Leaderboard.java b/src/main/java/uta/cse3310/Leaderboard.java index 226ea8f..7cd836e 100644 --- a/src/main/java/uta/cse3310/Leaderboard.java +++ b/src/main/java/uta/cse3310/Leaderboard.java @@ -5,36 +5,31 @@ public class Leaderboard { public TreeMap LB; - public Map sortedScore; + public SortedSet sortedScore; - public static > Map sortScores(final Map map) - { - Comparator valueComparator = new Comparator() - { - public int compare(K k1, K k2) - { - int comp = map.get(k1).compareTo(map.get(k2)); - if (comp == 0) - return 1; - else - return comp; - } - }; - Map sorted = new TreeMap(valueComparator); - sorted.putAll(map); - return sorted; - } + static > SortedSet> sortingScore(Map map) { + SortedSet> sortedEntries = new TreeSet>( + new Comparator>() { + @Override public int compare(Map.Entry e1, Map.Entry e2) { + int res = e1.getValue().compareTo(e2.getValue()); + return res != 0 ? res : 1; // Special fix to preserve items with equal values + } + } + ); + sortedEntries.addAll(map.entrySet()); + return sortedEntries; + } public Leaderboard() { // Initialize the TreeMap with a custom comparator to sort by score in descending order LB = new TreeMap<>(); - sortedScore = new Map(); + sortedScore = new SortedSet(); } // Method to add a score to the leaderboard public void add(String handle, int score) { LB.put(handle, score); sortedScore.clear(); - sortedScore = sortScores(LB); + sortedScore = sortingScore(LB); } public void update(String handle, int score) @@ -42,7 +37,7 @@ public void update(String handle, int score) int new_score = score + LB.get(handle); LB.put(handle, new_score); sortedScore.clear(); - sortedScore = sortScores(LB); + sortedScore = sortingScores(LB); } public void remove(String handle) @@ -51,7 +46,7 @@ public void remove(String handle) { LB.remove(handle); sortedScore.clear(); - sortedScore = sortScores(LB); + sortedScore = sortingScores(LB); } } }