From 552de95adda64daf7605229c19d3539fb8327acf Mon Sep 17 00:00:00 2001 From: Cool Guy <4052244-CoolGuy27@users.noreply.replit.com> Date: Wed, 13 May 2026 20:00:22 +0000 Subject: [PATCH] sort by goals --- HockeyStats.java | 11 ++++++++--- PlayerList.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/HockeyStats.java b/HockeyStats.java index 5be0823..dd35e4f 100644 --- a/HockeyStats.java +++ b/HockeyStats.java @@ -280,8 +280,9 @@ public class HockeyStats { (2) Find Player (3) Sort By Name (4) Sort By Points - (5) Show by Goals > num - (6) Return to Main Menu + (5) Sort By Goals + (6) Show by Goals > num + (7) Return to Main Menu <----------------------------> """; @@ -327,6 +328,10 @@ public class HockeyStats { JOptionPane.showMessageDialog(null, "Sorted by points"); break; case 5: + players.sortByGoals(); + JOptionPane.showMessageDialog(null, "Sorted by goals"); + break; + case 6: String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:"); if (minInput == null) { break; @@ -343,7 +348,7 @@ public class HockeyStats { JScrollPane showScrollPane = new JScrollPane(showTextArea); JOptionPane.showMessageDialog(null, showScrollPane); break; - case 6: + case 7: statsExit = true; break; default: diff --git a/PlayerList.java b/PlayerList.java index 0876569..5b37c1d 100644 --- a/PlayerList.java +++ b/PlayerList.java @@ -129,6 +129,21 @@ public class PlayerList { } } + /** Sorts the players in the list by total goals using an Insertion Sort algorithm. */ + public void sortByGoals() { + int n = pList.size(); + for (int i = 0; i < n; i++) { + HockeyPlayer key = pList.get(i); + int j = i - 1; + + while (j >= 0 && pList.get(j).getGoals() < key.getGoals()) { + pList.set(j + 1, pList.get(j)); + j--; + } + pList.set(j + 1, key); + } + } + /** * Locates a player and updates their stats with results from a single game. *