I am trying to sort ArrayList w.r.t termDate ArrayList contains firstName,lastName,email,startDate,termDate
Dates can be either empty or null. I have to compare accordingly and put all the null/empty date values at the end.
Collections.sort(usersList, new Comparator<User>() {
public int compare(User o1, User o2) {
if(o1.getTermDate() == null && o2.getTermDate() == null)
return 0; //They are both null, both equal
if(o1.getTermDate() == null && o2.getTermDate() != null)
return -1; // The first is null and the second is not, return the first as lower than the second
if(o1.getTermDate() != null && o2.getTermDate() == null)
return 1; //The first is not null and the second is, return the first as higher than the second
else
return o1.getTermDate().compare(o2.getTermDate()); //Return the actual comparison
}
});
It is not compiling compare method.
Please guide.
compareTomethod and notcompare.Collections.sortsorts the list in-place. That means the list given as argument will be sorted after the call to this method.