Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you say <code>distance</code> is a double, you likely have the same problem as described here:</p> <p><a href="https://stackoverflow.com/questions/9469308/java-error-comparison-method-violates-its-general-contract">Java error: &quot;Comparison method violates its general contract!&quot;</a></p> <p>Perhaps:</p> <pre><code>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; } } </code></pre> <p><strong><em>However</em></strong> 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.</p> <h2><strong>Using existing comparison methods</strong></h2> <p>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:</p> <pre><code>public int compareTo(Object o) { Match m = (Match) o; return m.matches == matches ? Double.compare(m.distance, distance) : Integer.compare(m.matches, matches); } </code></pre> <p>This way, <code>Double.compare</code> will handle the NaN values for you. For any number <code>x</code> (other than NaN) <code>Double.compare(x, Double.NaN) == -1</code> will return true (i.e., NaN is considered greater than any other number).</p> <p>Note here that you are OK using <code>==</code> with <code>int</code>s but it is more complicated with <code>double</code> because <code>Double.NaN != Double.NaN</code>. However, <code>new Double(Double.NaN).equals(Double.NaN)</code> is true. See <a href="https://stackoverflow.com/questions/1726254/why-is-javas-double-comparedouble-double-implemented-the-way-it-is">Why is Java&#39;s Double.compare(double, double) implemented the way it is?</a> for a nice discussion.</p> <h2><strong>Contract breaking:</strong></h2> <p>To see an example of why your original implementation might break the contract if you have NaNs, see <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo%28T%29" rel="nofollow noreferrer">Java compareTo documentation</a>. There we have:</p> <blockquote> <p>Finally, the implementer must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.</p> </blockquote> <p>So imagine you have <code>x = NaN</code> and <code>y = 5</code> and <code>z = 6</code>, then:</p> <ol> <li><code>x.compareTo(y) == 0</code> (since <code>NaN &gt; 5</code> and <code>NaN &lt; 5</code> are false)</li> <li><code>x.compareTo(z) == 0</code> (same reasoning)</li> <li><code>y.compareTo(z) == -1</code> (y &lt; z).</li> </ol> <p>So 2 and 3 (+<code>sgn</code>) are not equal as required.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload