• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

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;
}
0
kristian_gr
Asked:
kristian_gr
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now