From 5c63af46695e2fc903d45631461435d4ad99baf1 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Wed, 11 Jan 2023 15:01:28 -0600 Subject: [PATCH] Arrays have been defricked --- .../Lab2DArraysSort_CalebFontenot.java | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java index 3536c26..5179ba7 100644 --- a/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java +++ b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java @@ -43,7 +43,7 @@ public class Lab2DArraysSort_CalebFontenot { { for (int x = 0; x < ar.length; ++x) { for (int y = 0; y < ar[x].length; ++y) { - System.out.print(ar[x][y] + " "); + System.out.print(ar[x][y] + ""); } System.out.println(); } @@ -56,19 +56,13 @@ public class Lab2DArraysSort_CalebFontenot { * @param names the names to be sorted */ public static void sortNames(char[][] names) { - for (int arrayDim = 0; arrayDim < names[0].length; ++arrayDim) { - for (int i = 0; i < names[arrayDim].length - 1; ++i) { - for (int j = i + 1; j < names[arrayDim].length; ++j) { - // Compare all chars in string if first chars match - char compChar1 = names[arrayDim][i].charAt(0), compChar2 = names[arrayDim][j].charAt(0); - if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) { - compChar1 = names[arrayDim][i].charAt(1); - compChar2 = names[arrayDim][j].charAt(1); - - // Note: I'm not happy with this implementation, but it works as long as you're only having to compare 1 other character. - } + for (int i = 0; i < names.length - 1; ++i) { + for (int j = i + 1; j < names.length; ++j) { + char compChar1 = names[i][0], compChar2 = names[j][0]; + // Reoder entire row + for (int rowIterate = 0; rowIterate < maxNumberOfColumnsInJagged2dArray(names); ++rowIterate) { if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) { - String temp = names[i]; + char[] temp = names[i]; names[i] = names[j]; names[j] = temp; } @@ -76,21 +70,18 @@ public class Lab2DArraysSort_CalebFontenot { } } } - public static void main(String[] args) { char[][] names = { {'j', 'o', 'h', 'n'}, {'a', 'n'}, {'b', 'y', 'r', 'o', 'n'},}; - System.out.println(maxNumberOfColumnsInJagged2dArray(names)); //printColumnMajorOrder(names); printRowMajorOrder(names); - sortNames(names); System.out.println(); + sortNames(names); printRowMajorOrder(names); //printColumnMajorOrder(names); } } -}