String[][] 2dArray = new String[counter][2];
2dArray [counter][column1] = String.valueOf(counter);
2dArray [counter][column2] = "something something something";
for(int i = 0; i < 2dArray.length-1; i++){
for(int j = i + 1; j > 0; j--){
if(2dArray[i][j] < 2dArray[i-1][j]){
int[][] temp = 2dArray[i-1][j];
2dArray[i-1][j] = 2dArray[i][j];
2dArray[i][j] = temp;
}
}
}
Attempting to sort the array so that column 1 is ascending. I've studied the other references on here and mimic'd them but for some reason my IDE does not like the above...
If 2dArray is a two dimensional String array like
String[][] 2dArray
then the two strings can be compared using
if(2dArray[i][j].compareTo(2dArray[i-1][j]) > 0)
If I understand you correctly, I would suggest the following:
What you would need to do is to compare the
Integer
values of the array:The reason you don't include
j
is because you are only sorting by the value of the first column.2dArray[i][0]
gets you the value of yourcounter
at that particular row.I've also seen some other stuff in your code that could use fixing:
This is more in line with what I think is the classic implementation of BubbleSort:
Except in your case, I'm treating your array as a one-dimensional, since you are only sorting by one dimension.
Would this also fix it ?
Your IDE doesn't like you (120%) when you put a char/int/... (per first response) into a two dimension char array.