I have a single demenational array with student names and a 2D array with student marks, I can sort the names fine but I cannot get the marks to match (as they have to stay in the same order). Here is my attempted code:
static String[] studentNamesArray = new String[10];
static int[][] studentMarksArray = new int[10][3];
static void sortAlphabetical() {
String tempName;
int intSwap;
boolean flag = false;
while (flag==false) {
flag = true;
for (int i = 0; i < 9; i++) {
if (studentNamesArray[i].compareTo(studentNamesArray[i + 1])>0) {
tempName = studentNamesArray[i];
studentNamesArray[i] = studentNamesArray[i + 1];
studentNamesArray[i + 1] = tempName;
for(int y=0;y<2;y++){
intSwap = studentMarksArray[i][0];
studentMarksArray[i][y] = studentMarksArray[i+1][y+1];
studentMarksArray[i+1][y+1] = intSwap;
}
flag = false;
}
}
}
}
Student
object holding its name and its marks, then create aStudent[]
array and sort it by name using aComparator
? – Alexis C. Nov 28 '13 at 15:58compareTo
is a OO method... – brimborium Nov 28 '13 at 16:07