I think many people have come across this problem, You have a 2D array consisting of a String conponent and an Interger. ex.
`String[][] data = {{"Name1","5"}, {"Name2","10"},{"Name3","1"}, {"Name4","3"}};`
Now you want to sort that 2D array by the integer (in this case the players score), however you want the matching name to be moved along with the players score. This is what i've got, but the result is far away from what it is suppose to be.
private void sort(){
boolean sort;
int current, next;
do{
sort = true;
for (int i = 0; i < data.length - 1; i++){
if (data[i][1] !=null && data[i+1][1] != null){
current = Integer.parseInt(data[i][1]);
next = Integer.parseInt(data[i+1][1]);
if(current > next){
String temp = "";
data[i][1] = Integer.toString(next);
data[i+1][1] = Integer.toString(current);
data[i][0] = temp;
data[i][0] = data[i+1][1];
data[i+1][0] = temp;
sort = false;
}
}
}
}while(!sort);
}
If you ask why people would use a common 2D array, it is because in JFRAME, a JTable needs an 2D array for the data.