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 8225199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:20:12+00:00 2026-06-07T15:20:12+00:00

I am trying to implement the comparable interface in Java for an object that

  • 0

I am trying to implement the comparable interface in Java for an object that I need to sort by two different columns/variables. I tried multiple approaches, and this one is the best so far:

public int compareTo(Object o) {
    Match m = (Match)o;
    int diff = m.matches - matches;
    if (diff == 0) {
        if (distance > m.distance) {
            return 1;
        } else if (distance < m.distance) {
            return -1;
        } else {
            return 0;
        }
    } else {
        return diff;
    }
}

but it still fails with

java.lang.IllegalArgumentException: Comparison method violates its general contract!

Any ideas what I’m doing wrong?

Side note 1: NPEs/ClassCastExceptions are expected, if o is null or of an unsuitable class – that is not the issue here.

Side note 2: I know about the change in the sorting algorithm in JDK 1.7, but I don’t really see where I’m violating the contract here. So turning off the exception seems like the wrong solution.

  • 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-06-07T15:20:13+00:00Added an answer on June 7, 2026 at 3:20 pm

    Since you say distance is a double, you likely have the same problem as described here:

    Java error: "Comparison method violates its general contract!"

    Perhaps:

    public int compareTo(Object o) {
        Match m = (Match)o;
        int diff = m.matches - matches;
        if (diff == 0) {
            return Double.compare(distance, m.distance);
        } else {
            return diff;
        }
    }
    

    However ideally you should use the built-in comparison methods as I state below.
    The above code is an example of the “minimum change needed”, illustrating the key problem.

    Using existing comparison methods

    Also, as @fabian-barney states in his answer, you should avoid taking the direct difference, and instead utilise the built-in comparison methods. So you should have something like:

    public int compareTo(Object o) {
        Match m = (Match) o;
        return m.matches == matches ? Double.compare(m.distance, distance) : Integer.compare(m.matches, matches);
    }
    

    This way, Double.compare will handle the NaN values for you. For any number x (other than NaN) Double.compare(x, Double.NaN) == -1 will return true (i.e., NaN is considered greater than any other number).

    Note here that you are OK using == with ints but it is more complicated with double because Double.NaN != Double.NaN. However, new Double(Double.NaN).equals(Double.NaN) is true. See Why is Java's Double.compare(double, double) implemented the way it is? for a nice discussion.

    Contract breaking:

    To see an example of why your original implementation might break the contract if you have NaNs, see Java compareTo documentation. There we have:

    Finally, the implementer must ensure that x.compareTo(y)==0 implies
    that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

    So imagine you have x = NaN and y = 5 and z = 6, then:

    1. x.compareTo(y) == 0 (since NaN > 5 and NaN < 5 are false)
    2. x.compareTo(z) == 0 (same reasoning)
    3. y.compareTo(z) == -1 (y < z).

    So 2 and 3 (+sgn) are not equal as required.

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

Sidebar

Related Questions

I am trying to implement generics in Java using Comparable<T> interface. public static <T>
I have a Java question. I'm trying to implement Comparable in my class. Based
I've got a class, Accumulator, that implements the Comparable compareTo method, and I'm trying
Alright, so I've been trying to implement a simple binary search tree that uses
I am trying to implement an inner class that has a generic parameterized type.
I'm trying to implement a search method, which returns a index of a object
I'm trying to implement a sorted list as a simple exercise in Java. To
I'm trying to write a program that accepts type comparable into the DataSet class
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I am trying implement a most recent widget into my tumblr post. So far,

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.