I'm using Java and I have an int[][] array like so:
[ 65][ 4]
[108][ 47]
[ 32][279]
[103][ 26]
[111][138]
[100][ 63]
[112][ 33] ...etc.
And I need to sort from least to greatest, based on the second column's values. I tried this code, also found on this website:
print(myArray);
System.out.println("==========");
Arrays.sort(myArray, new Comparator<int[]>() {
@Override
public int compare(int[] int1, int[] int2)
{
Integer number1 = int1[1];
Integer number2 = int2[1];
return number1.compareTo(number2);
}
});
print(myArray);
Where my print method is as follows:
public static void print(int[][] array) {
int k = 0;
while (array[k][0] != 0) {
System.out.println("[" + array[k][0] + "][" + array[k][1] + "]");
k++;
}
}
It just doesn't seem to print the second time, no matter what. I'm just not sure what I'm doing wrong here. Hopefully it's just an easy fix :)