The class imported as dependency in my project doesnt override compareTo method. Is there a way i can create this sorting logic on my own without request a change on the dependent project.
Asked
Active
Viewed 73 times
1
-
1http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – s7vr Oct 22 '16 at 17:19
2 Answers
1
Yes - you can always implement a Comparator
yourself:
public class SomeClassComparator implements Comparator<SomeClass> {
@Override
int compare (SomeClass o1, SomeClass o2) {
// Just an example
return Integer.compare(o1.getSomeInt(), o2.getSomeInt());
}
}

Mureinik
- 297,002
- 52
- 306
- 350
0
You can do it by using Comparator
Collections.sort(yourList, new Comparator<YourObject>() {
public int compare(YourObject o1, YourObject o2) {
return o2.getValue().compareTo(o1.getValue());
}
});

NullPointerException
- 3,732
- 5
- 28
- 62