Arrays have been defricked
This commit is contained in:
parent
3c32d0a20b
commit
5c63af4669
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user