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 first column?
I tried nesting the loops as follows but it's not working! What am I doing wrong??
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).getSchoolCode(); //Extract 1st
s2 = loads.get(j).getSchoolCode(); //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).getLecturerID();
s2 = loads.get(n).getLecturerID();
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
}//end loop
return loads;
}
I know I can use a Comparator to do this, but I don't want to.
Any help welcome?
Many thanks TeachingLoad held in arraylist is:
public TeachingLoad(String schoolCode, String departmentNumber,
String lecturerID, String lecturerName,
String moduleNo, String creditHours, String classSize) {
this.schoolCode = schoolCode;
this.departmentNumber = departmentNumber;
this.lecturerID = lecturerID;
this.lecturerName = lecturerName;
this.moduleNo = moduleNo;
this.creditHours = creditHours;
this.classSize = classSize;
}
public String getSchoolCode() {
return schoolCode;
}
public String getDepartmentNumber(){
return departmentNumber;
}
public String getModuleNo() {
return moduleNo;
}
public String getCreditHours() {
return creditHours;
}
public String getClassSize() {
return classSize;
}
public String getLecturerID() {
return lecturerID;
}
public String getLecturerName() {
return lecturerName;
}
public void setLecturerID(String lecturerID) {
this.lecturerID = lecturerID;
}
public void setLecturerName(String lecturerName) {
this.lecturerName = lecturerName;
}
public void setSchoolCode(String schoolCode) {
this.schoolCode = schoolCode;
}
public void setDepartmentNumber(String departmentNumber){
this.departmentNumber = departmentNumber;
}
public void setModuleNo(String moduleNo) {
this.moduleNo = moduleNo;
}
public void setCreditHours(String creditHours) {
this.creditHours = creditHours;
}
public void setClassSize(String classSize) {
this.classSize = classSize;
}
ETC
Cheers