sort by goals

This commit is contained in:
Cool Guy
2026-05-13 20:00:22 +00:00
parent d73e5d29fd
commit 552de95add
2 changed files with 23 additions and 3 deletions

View File

@@ -280,8 +280,9 @@ public class HockeyStats {
(2) Find Player (2) Find Player
(3) Sort By Name (3) Sort By Name
(4) Sort By Points (4) Sort By Points
(5) Show by Goals > num (5) Sort By Goals
(6) Return to Main Menu (6) Show by Goals > num
(7) Return to Main Menu
<----------------------------> <---------------------------->
"""; """;
@@ -327,6 +328,10 @@ public class HockeyStats {
JOptionPane.showMessageDialog(null, "Sorted by points"); JOptionPane.showMessageDialog(null, "Sorted by points");
break; break;
case 5: case 5:
players.sortByGoals();
JOptionPane.showMessageDialog(null, "Sorted by goals");
break;
case 6:
String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:"); String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:");
if (minInput == null) { if (minInput == null) {
break; break;
@@ -343,7 +348,7 @@ public class HockeyStats {
JScrollPane showScrollPane = new JScrollPane(showTextArea); JScrollPane showScrollPane = new JScrollPane(showTextArea);
JOptionPane.showMessageDialog(null, showScrollPane); JOptionPane.showMessageDialog(null, showScrollPane);
break; break;
case 6: case 7:
statsExit = true; statsExit = true;
break; break;
default: default:

View File

@@ -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. * Locates a player and updates their stats with results from a single game.
* *