1
ArrayList的數據例如: BJM 300 AC4507 TOM_JONES,BDM 290 DC4058 ALAN_FIELD,ADG 350 BA3240 JON_THORN多種排序算法中的Java(已排序的字段內分選)
我需要上述的ArrayList分類到的升序順序第三列內第二列第三列?
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;
}
上面是我有ATM的代碼。這種排序的第一列(BJM,BDM & ADG列),但我將不得不做的排序內排序的數據,如上所述??我認爲排序3次,但這不起作用嗎?
我已經試過嵌套排序(見下文),下文提到的,但沒有喜悅:
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;
}
「第三列中的第二列中的第三列的」這是什麼意思? –