I want to sort a List of Objects (Remarks) by a value contained in another object (DefectClass).
public class Remark {
private int remarkId;
private DefectClass defectClass;
}
I have a List of Remarks and I'm using this code to sort them by the title of the defectClass :
Collections.sort(remarks, new Comparator<Remark>()
{
@Override
public int compare(Remark arg0, Remark arg1) {
return arg0.getDefectClass().compareTo(arg1.getDefectClass());
}
});
and this is the Compare to methode in my DefectClass model :
public class DefectClass implements Comparable<DefectClass> {
private String DefectClassTitle;
/* Getters And Setters */
@Override
public int compareTo(DefectClass o) {
if(o.getDefectClassTitle().equals(this.getDefectClassTitle()))
return 0;
else
return 1;
}
by these codes I want to sort a List of remarks by the Title of the DefectClass of these Remarks ... but At the end I always get the same list with no sorting. What I'm doing wrong ?
Collections.sort()with newComparator2.)Collections.sort()for the class that implementsComparable.return o.getDefectClassTitle().compareTo(this.getDefectClassTitle()). You're never returning-1otherwise.compareTocontract, you must return-1,0and1.