File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Comparator & Equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparator & Equals" Watch "Comparator & Equals" New topic

Comparator & Equals

Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
I understand that as a general rule of dumb an objects comparator method should be consistent with the objects equals method. Now what if I want to sort on some common attributes that a list may have. And this attribute does not determine whether an object is equal or not. In other words is it any way possible for me to use a comparator where compare(o1,o2) == 0 and o1.equals(o2) == false?
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
My understanding is that you can sort with Comparitor not consistant with equals, if the underlying collection is not using equals in a way inconsistant with what the search is trying to do (as is the case in the example they give involving search). But I've never tried it, nor have I fully researched it.
[ June 17, 2002: Message edited by: John Dale ]
I agree. Here's the link:
subject: Comparator & Equals
It's not a secret anymore!