Joanne
Joanne
Dennis Deems wrote:You can always write a comparator external to the class it compares. In fact I prefer this, as I find classes that implement their own comparator to be kind of ugly.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Dennis Deems wrote:You can always write a comparator external to the class it compares. In fact I prefer this, as I find classes that implement their own comparator to be kind of ugly.
Hmmm. Not so sure about that. Firstly, what is equals() if not a form of comparison? Also, some classes (eg, Integer, String ...) simply scream "Comparable" to me.
Dennis Deems wrote:You can always write a comparator external to the class it compares. In fact I prefer this, as I find classes that implement their own comparator to be kind of ugly.
Joanne
Dennis Deems wrote:I said "Comparator", not "Comparable". An instance of a class which implements Comparable can compare itself to another instance. A Comparator is a utility that can compare two instances of some class.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Dennis Deems wrote:I said "Comparator", not "Comparable". An instance of a class which implements Comparable can compare itself to another instance. A Comparator is a utility that can compare two instances of some class.
Erm, yes. I do understand the distinction.
Perhaps I didn't understand your "I find classes that implement their own comparator to be kind of ugly" statement.
A Comparator is simply an alternate ordering. Nothing more, nothing less. Perhaps it's my background in databases, but I understand that tables (or collections) in Java cannot always be ordered the way we would like (cf. secondary indexes in a database). My solution simply allows any ordering, including the class's own, to be provided by a Comparator. Don't you think that might make the design of a new ordered collection or sort more straightforward?
Consider Paul's rocket mass heater. |