ARTICLE AD BOX
I'm trying to make it so my history entries from my save file are being sorted properly but nothing displays. The first option does work, however, as it displays all 50 history entries randomly as expected. I cannot find out why it's not sorting. Here are my 2 main files. (Sorry if I'm missing something or sound stupid, I'm very new to sorting.)
Snippet 1 of the method I'm trying to do the sorting in:
else if (choice == 2) { // sort by account types for (int i = 0; i < HistoryManager.globalTransHistory.size() - 1; i++) { for (int j = 0; j < HistoryManager.globalTransHistory.size() - i - 1; j++) { String entry1 = HistoryManager.globalTransHistory.get(j); String entry2 = HistoryManager.globalTransHistory.get(j + 1); String[] parts1 = entry1.split("\\|"); String[] parts2 = entry2.split("\\|"); if (parts1.length < 2 || parts2.length < 2) continue; String type1 = parts1[1].replace("account type:", "").trim(); // "chequing" String type2 = parts2[1].replace("account type:", "").trim(); if (type1.compareTo(type2) > 0) { // swapping HistoryManager.globalTransHistory.set(j, entry2); HistoryManager.globalTransHistory.set(j + 1, entry1); } } } System.out.println("\n======= TRANSACTION HISTORY ======="); for (String entry : HistoryManager.globalTransHistory) { System.out.println(entry); } System.out.println("===================================\n"); return; }The full other class:
import java.util.ArrayList; public class HistoryManager { static ArrayList<String> globalTransHistory = new ArrayList<>(); // single method to log any action public static void logHistEntry(String actionType, int accountNum, String accountType, double amount) { String timestamp = java.time.LocalDateTime.now() .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String entry = timestamp + "| account type: " + accountType + " | account#" + accountNum + " | " + actionType + " | amount $" + amount; globalTransHistory.add(entry); } // public static void logAndTransferHistory(int fromAcc, int toAcc, double amount, double fromBalance, double toBalance) { // String timestamp = java.time.LocalDateTime.now().toString(); // String entry = timestamp + " | account#" + accountNum + " | " + actionType + " | $" + amount + " | balance: $" + newBalance; // globalHistory.add(entry); // } }