Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
Nightly Builds
-
None
-
None
-
None
-
Operating System: other
Platform: Other
-
18791
Description
Assume a "Person" with 3 string properties age, name, hairColor. We've got
three people, Bob, Jane, and John - John happens to be bald, and his hairColor
property is "null". Trying to sort a collection of Person beans by hairColor
with a null entry throws a ClassCastException from ComparableComparator.
" java.lang.ClassCastException: There were nulls in the arguments for this
method: compare(hawkeye, null)' "
One should be able to sort a collection of beans on a property which contains
null values. The question that remains is whether the comparing with a null
should return a 1 or a -1 (do you want your nulls in the beginning or at the
end?). I'd assume at the end as the default behaviour - maybe add a property
to allow people to customize this.
When I originally wrote the BeanComparator, I had that type of functionality,
but it was removed for the initial commit because of exactly that issue. Who
comes first, the null or the not null value?
To get around this, I wrote a NullComparator that allows you to specify who
comes first, the null or the not null value. I then wrapped the bean
comparator in the null comparator using the comparatorChain.
I think add this functionality directly confuses the BeanComparator. And also,
you could argue other comparators suffer from the same issue with nulls. If
you want to see some code, let me know.
Eric