Arrays have been defricked

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2023-01-11 15:01:28 -06:00
parent 3c32d0a20b
commit 5c63af4669

View File

@ -43,7 +43,7 @@ public class Lab2DArraysSort_CalebFontenot {
{ {
for (int x = 0; x < ar.length; ++x) { for (int x = 0; x < ar.length; ++x) {
for (int y = 0; y < ar[x].length; ++y) { for (int y = 0; y < ar[x].length; ++y) {
System.out.print(ar[x][y] + " "); System.out.print(ar[x][y] + "");
} }
System.out.println(); System.out.println();
} }
@ -56,19 +56,13 @@ public class Lab2DArraysSort_CalebFontenot {
* @param names the names to be sorted * @param names the names to be sorted
*/ */
public static void sortNames(char[][] names) { public static void sortNames(char[][] names) {
for (int arrayDim = 0; arrayDim < names[0].length; ++arrayDim) { for (int i = 0; i < names.length - 1; ++i) {
for (int i = 0; i < names[arrayDim].length - 1; ++i) { for (int j = i + 1; j < names.length; ++j) {
for (int j = i + 1; j < names[arrayDim].length; ++j) { char compChar1 = names[i][0], compChar2 = names[j][0];
// Compare all chars in string if first chars match // Reoder entire row
char compChar1 = names[arrayDim][i].charAt(0), compChar2 = names[arrayDim][j].charAt(0); for (int rowIterate = 0; rowIterate < maxNumberOfColumnsInJagged2dArray(names); ++rowIterate) {
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.
}
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) { if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
String temp = names[i]; char[] temp = names[i];
names[i] = names[j]; names[i] = names[j];
names[j] = temp; names[j] = temp;
} }
@ -76,21 +70,18 @@ public class Lab2DArraysSort_CalebFontenot {
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
char[][] names = { char[][] names = {
{'j', 'o', 'h', 'n'}, {'j', 'o', 'h', 'n'},
{'a', 'n'}, {'a', 'n'},
{'b', 'y', 'r', 'o', 'n'},}; {'b', 'y', 'r', 'o', 'n'},};
System.out.println(maxNumberOfColumnsInJagged2dArray(names));
//printColumnMajorOrder(names); //printColumnMajorOrder(names);
printRowMajorOrder(names); printRowMajorOrder(names);
sortNames(names);
System.out.println(); System.out.println();
sortNames(names);
printRowMajorOrder(names); printRowMajorOrder(names);
//printColumnMajorOrder(names); //printColumnMajorOrder(names);
} }
} }
}