Neroku wrote:
> Well, I know equals() and compareTo() could behave different to test
> for equality, it just depend on the way I implement them, but, Would it
> be a bad practice to rewrite equals() and compareTo() so they behave in
> the following way:
>
> - Two different instances of the same class hold different attribute
> values, calling equals() return false comparing both objects because
> they are not the same, but compareTo() return 0, since this is the
> sorting criteria I want (a desired attribute match).
I would say that in general it's a bad idea for compareTo() not to be
compatible with equals(). As others have said, it's not what they'd expect to
see, and that's always something to avoid if you can.
There may be sufficient reasons to break that rule (though I can't think of an
example off the top of my head). Remember that you have the option of using a
custom Comparator for your objects instead of the natural ordering implied by
compareTo(). If the "odd" comparison order is particularly natural (despite
it's oddness

or likely to be needed in several places, then I would make
the special Comparator implementation a class statically nested within the main
object class. Quite possibly the Comparator would be stateless in which case
you could have a single, immutable, public instance of the Comparator available
for convenient use.
-- chris