When using Collection.sort in Java what should I return when one of the inner objects is null
Example:
Collections.sort(list, new Comparator
Naturally, it's your choice. Whatever logic you write, it will define sorting rules. So 'should' isn't really the right word here.
If you want null to appear before any other element, something like this could do
public int compare(MyBean o1, MyBean o2) {
if (o1.getDate() == null) {
return (o2.getDate() == null) ? 0 : -1;
}
if (o2.getDate() == null) {
return 1;
}
return o2.getDate().compareTo(o1.getDate());
}