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;
}
kristian_grAsked:
Who is Participating?
 
mnrzCommented:
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;
                  }

}
);

sorry I forget a ) here
0
 
mnrzCommented:
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;
                  }

}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.