Number Comparison Dilemma in Java
Wednesday, April 29, 2009 4:36:15 PM
At first I thought that the Number class implements the Comparable interface in a way that makes it possible to sort a collection of heterogenous Number objects. To my surprise, it turned out that the Number class does not implement the Comparable and instead, implementing Comparable is delegated to its subclasses. So an Integer is only comparable to an Integer, a Double to a Double, and so on.
It turns out that this is not a new problem and it has hit a lot of other programmers as well. In short, there's not a concise and clean solution to this problem. I searched through the Web and I came up with these two links among others that discuss this very problem and I thought it worths to share it here:
- Java Bug Parade entry (dated 12 Feb 2001)
java.lang.Number should implement java.lang.Comparable.
- Stack Overflow
Why doesn’t java.lang.Number implement Comparable?
If you have any thoughts, ideas, or solutions please care to write about them in the comments.