ArrayList data example: BJM 300 AC4507 TOM_JONES, BDM 290 DC4058 ALAN_FIELD, ADG 350 BA3240 JON_THORN
I need to sort the ArrayList above into ascending sequence of the third column within the second column within the third column?
public static ArrayList sortLoad1(ArrayList<WorkLoad> loads){
String s1, s2;
WorkLoad temp; //Some local variables
for(int i = 0 ; i < loads.size(); i++){ //Loop forward
for(int j = loads.size()-1; j>i ;j--){ //Loop backward
s1 = loads.get(j-1).getDeptCode(); //Extract 1st
s2 = loads.get(j).getDeptCode(); //Extract 2nd
if(i+1<loads.size()&&s1.compareTo(s2)>-1){ //Compare them lexicographically
temp = loads.get(j-1);
//If s1 follows s2 then switch both
loads.set(j-1, loads.get(j));
loads.set(j, temp);
}//endif
}//end loop 2
}//end loop 1
return loads;
}
Above is the code I have ATM. This sorts the first column (BJM, BDM & ADG columns) but what would I have to do to sort within the sorted data as I mentioned above?? I thought sorting 3 times, but that's not going to work is it?
I've tried the nested sort (see below) mentioned below but no joy:
public static ArrayList sortLoad1(ArrayList<TeachingLoad> loads){
String s1, s2;
TeachingLoad temp; //Some local variables
for(int i = 0 ; i < loads.size(); i++){ //Loop throuth
for(int j = loads.size()-1; j>i ;j--){ //Loop through
s1 = loads.get(j-1).getLecturerID(); //Extract 1st
s2 = loads.get(j).getLecturerID(); //Extract 2nd
if(i+1<loads.size()&&s1.compareTo(s2)>-1){ //Compare them lexicographically
temp = loads.get(j-1);
//If s1 follows s2 then switch both
loads.set(j-1, loads.get(j));
loads.set(j, temp);
}
else{
for(int k = 0 ; k < loads.size(); k++){
for(int l = loads.size()-1; l>i ;l--){
s1 = loads.get(l-1).getDepartmentNumber();
s2 = loads.get(l).getDepartmentNumber();
if(k+1<loads.size()&&s1.compareTo(s2)>-1){
temp = loads.get(l-1);
loads.set(l-1, loads.get(l));
loads.set(l, temp);
}
else{
for(int m = 0 ; m < loads.size(); m++){
for(int n = loads.size()-1; n>i ;n--){
s1 = loads.get(n-1).getSchoolCode();
s2 = loads.get(n).getSchoolCode();
if(m+1<loads.size()&&s1.compareTo(s2)>-1){
temp = loads.get(n-1);
loads.set(n-1, loads.get(n));
loads.set(n, temp);
}
}
}
}
}
}
}
}//end loop 2
}//end loop 1
return loads;
}