From 33c3935181e5dcd80a33b0b9672298b804e52a28 Mon Sep 17 00:00:00 2001 From: giorginamarie Date: Tue, 21 Oct 2025 18:57:13 -0600 Subject: [PATCH] Added descending sort to Game History --- GameHistoryTracker.java | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/GameHistoryTracker.java b/GameHistoryTracker.java index ffd810e..b7d6a3a 100644 --- a/GameHistoryTracker.java +++ b/GameHistoryTracker.java @@ -1,4 +1,3 @@ -import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.io.IOException; @@ -35,7 +34,8 @@ public void recordPlay(final String gameName, final Integer score) { } /** - * Displays a summary of play history and scores. + * Displays a summary of play history sorted in descending order + * along with corresponding scores, if any. */ public void displayHistory() { System.out.println("\n=== Game Play History ==="); @@ -43,16 +43,28 @@ public void displayHistory() { System.out.println("No games played yet."); return; } - for (Map.Entry entry : statsMap.entrySet()) { - String game = entry.getKey(); - GameStats stats = entry.getValue(); - System.out.printf("%s - Played: %d", game, stats.timesPlayed); - if (!stats.scores.isEmpty()) { - double avg = stats.totalScore / (double) stats.scores.size(); - System.out.printf(", Avg Score: %.2f", avg); - } - System.out.println(); - } + statsMap.entrySet().stream() + .sorted((a, b) -> { + int cmp = Integer.compare( + b.getValue().getTimesPlayed(), + a.getValue().getTimesPlayed() + ); + return (cmp == 0) + ? a.getKey().compareToIgnoreCase(b.getKey()) + : cmp; + }) + .forEach(entry -> { + String game = entry.getKey(); + GameStats stats = entry.getValue(); + System.out.printf("%s - Played: %d", + game, stats.getTimesPlayed()); + if (!stats.scores.isEmpty()) { + double avg = + stats.totalScore / (double) stats.scores.size(); + System.out.printf(", Avg Score: %.2f", avg); + } + System.out.println(); + }); } /**