TripVoltage
TripVoltage

Reputation: 341

Multiple sort algorithm (sorting within a sorted field) in Java

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;
}

Upvotes: 1

Views: 1031

Answers (1)

Tim B
Tim B

Reputation: 41208

You need to nest the sorts.

Essentially do the first comparison. If the result is not 0 then use that result.

If result is 0 then do the next comparison. Again if result is not 0 then use it.

You can continue this process for as many nested comparisons as you require.

The neatest way to implement it is as a custom comparator though. Then you can just do Collections.sort(list, comparitor) and it will efficiently and quickly sort the list using an appropriate algorithm for the list.

For example if you have:

class X {
   int a, b, c;
}

Comparator<X> comparator = new Comparator<X>() {
     public int compare(X one, X two) {
         int result = one.a-two.a;
         if (result == 0) {
             result = one.b-two.b;
             if (result == 0) {
                  result = one.c-two.c;
             }
         }
         return result;
     }
}

That will sort a list of X first by a, then by b, then by c.

To use it just do:

 Collections.sort(list, comparator);

Upvotes: 1

Related Questions