Link to home
Start Free TrialLog in
Avatar of kristian_gr
kristian_grFlag for Norway

asked on

Sorting a List of objects without implements Comparable

Hi.
I'm tying to figure out how to sort a list of objects ex List<Test> testList. Usually I'd do "public class Test implements Comparable<Test>" and use the CompareTo method as Colllections.sort(testList).

But for other reasons my object can not impement Comparable. What is the easiest way to sort a list then?

I need the objects in my testList sorted on the variable sortNum.

test class looks like this:
public class Test() {
  private String Name;
  private int sortNum;
}
Avatar of mnrz
mnrz

you can use Comparator instead

Collections.sort(yourList, new Comparator<Test>() {

                  public int compare(Test t1, Test t2) {
                                // check whether these two Test are equal (0), greater than (1)or less than(-1)
                        return 0;
                  }

}
ASKER CERTIFIED SOLUTION
Avatar of mnrz
mnrz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial