kristian_gr
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;
}
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Collections.sort(yourList,
public int compare(Test t1, Test t2) {
// check whether these two Test are equal (0), greater than (1)or less than(-1)
return 0;
}
}