public int compare(Event e1, Event e2) {
if (e1 == null && e2 == null) {
return 0;
} else if (e1 == null && e2 != null) {
return -1;
} else if (e1 != null && e2 == null) {
return 1;
} else if (e1.getDate() == null && e2.getDate() == null) {
return 0;
} else if (e1.getDate() == null && e2.getDate() != null) {
return -1;
} else if (e1.getDate() != null && e2.getDate() == null) {
return 1;
} else
return e1.getDate().compareTo(e2.getDate());
}
Hey would like to ask about some better way of writing the code above for a comparator exercise. The requirements are to handle null values in a way where two null values are equal and a null value is always smaller(in comparison) to a non null value.
This code is totally functional but it just seems to me abit sketchy. would like to hear some opinions and mind openers here :)