How to sort 1D (String) array and 2D (int) array based on 1D (double) array with Bubble Sort in Java. I managed to sort String array based on double array but can't figure out how to also sort 2D (int) array. Every row in 2D array (grades) represents each students multiple grades. I need to achieve goal by using this kind of structure (three arrays). Everything needs to be sorted depending on finalGrade array.
static void sort(String[] students, int[][] grades, double[] finalGrade) {
double tempFG;
String tempStud;
int t;
//Bubble Sort
for (int i=0; i<students.length-1; i++) {
for (int j=0; j<finalGrade.length-i-1; j++) {
if (finalGrade[j] < finalGrade[j+1]) {
tempFG = finalGrade[j];
tempStud = students[j];
finalGrade[j] = finalGrade[j+1];
students[j] = students[j+1];
finalGrade[j+1] = tempFG;
students[j+1] = tempStud;
}
}
}
}
0ton-1, according to the sort criteria for the 2D array. Then use the index array to generate the sorted elements. The issue is that I've only written a C++ solution, but the principle is the same.