Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 669441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:09:05+00:00 2026-05-14T00:09:05+00:00

I believe this question applies equally well to C# as to Java, because both

  • 0

I believe this question applies equally well to C# as to Java, because both require that {c,C}ompareTo be consistent with {e,E}quals:

Suppose I want my equals() method to be the same as a reference check, i.e.:

public bool equals(Object o) {
    return this == o;
}

In that case, how do I implement compareTo(Object o) (or its generic equivalent)? Part of it is easy, but I’m not sure about the other part:

public int compareTo(Object o) {
    MyClass other = (MyClass)o;
    if (this == other) {
        return 0;
    } else {
        int c = foo.CompareTo(other.foo)
        if (c == 0) {
            // what here?
        } else {
            return c;
        }
    }
}

I can’t just blindly return 1 or -1, because the solution should adhere to the normal requirements of compareTo. I can check all the instance fields, but if they are all equal, I’d still like compareTo to return a value other than 0. It should be true that a.compareTo(b) == -(b.compareTo(a)), and the ordering should stay consistent as long as the objects’ state doesn’t change.

I don’t care about ordering across invocations of the virtual machine, however. This makes me think that I could use something like memory address, if I could get at it. Then again, maybe that won’t work, because the Garbage Collector could decide to move my objects around.

hashCode is another idea, but I’d like something that will be always unique, not just mostly unique.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:09:05+00:00Added an answer on May 14, 2026 at 12:09 am

    First of all, if you are using Java 5 or above, you should implement Comparable<MyClass> rather than the plain old Comparable, therefore your compareTo method should take parameters of type MyClass, notObject:

    public int compareTo(MyClass other) {
        if (this == other) {
            return 0;
        } else {
            int c = foo.CompareTo(other.foo)
            if (c == 0) {
                // what here?
            } else {
                return c;
            }
        }
    }
    

    As of your question, Josh Bloch in Effective Java (Chapter 3, Item 12) says:

    The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compare-
    To(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception
    if and only if y.compareTo(x) throws an exception.)

    This means that if c == 0 in the above code, you must return 0.

    That in turn means that you can have objects A and B, which are not equal, but their comparison returns 0. What does Mr. Bloch have to say about this?

    It is strongly recommended, but not strictly required, that (x.compareTo(y)
    == 0) == (x.equals(y)). Generally speaking, any class that implements
    the Comparable interface and violates this condition should clearly indicate
    this fact. The recommended language is “Note: This class has a natural
    ordering that is inconsistent with equals.”

    And

    A class whose compareTo method imposes an order
    that is inconsistent with equals will still work, but sorted collections containing
    elements of the class may not obey the general contract of the appropriate collection
    interfaces (Collection, Set, or Map). This is because the general contracts
    for these interfaces are defined in terms of the equals method, but sorted collections
    use the equality test imposed by compareTo in place of equals. It is not a
    catastrophe if this happens, but it’s something to be aware of.

    Update: So IMHO with your current class, you can not make compareTo consistent with equals. If you really need to have this, the only way I see would be to introduce a new member, which would give a strict natural ordering to your class. Then in case all the meaningful fields of the two objects compare to 0, you could still decide the order of the two based on their special order values.

    This extra member may be an instance counter, or a creation timestamp. Or, you could try using a UUID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.