<p>I'm unsure whether the following code would ensure all conditions given in Comparator's Javadoc. </p>

<pre><code>class TotalOrder&lt;T&gt; implements Comparator&lt;T&gt; {

    public boolean compare(T o1, T o2) {
        if (o1 == o2 || equal(o1, o2)) return 0;

        int h1 = System.identityHashCode(o1);
        int h2 = System.identityHashCode(o2);

        if (h1 != h2) {
            return h1 &lt; h2 ? -1 : 1;
        }

        // equals returned false but identity hash code was same, assume o1 == o2
        return 0;
    }

    boolean equal(Object o1, Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
    }
}
</code></pre>

<p>Will the code above impose a total ordering on all instances of any class, even if that class does not implement Comparable?</p>

[{'answer_id': 28343, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 1, 'selected': False, 'text': "<p>You answered in your comment: </p>\n\n<blockquote>\n  <p>equals returned false but identity hash code was same, assume o1 == o2</p>\n</blockquote>\n\n<p>Unfortunately you cannot assume that. Most of the time that is going to work, but in some exceptionnal cases, it won't. And you cannot know when. When such a case appear, it would lead to lose instances in TreeSets for example.</p>\n"}, {'answer_id': 28385, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 1, 'selected': False, 'text': "<p>I don't think it does since this clause is not met:</p>\n\n<blockquote>\n  <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>\n</blockquote>\n\n<p>Since equal(o1, o2) depends on o1's implementation of equals, two objects that are logically equal (as determined by equals) still have two differrent identityHashCodes.</p>\n\n<p>So when comparing them to a third object (z), they might end up yielding different values for compareTo.</p>\n\n<p>Make sense?</p>\n"}, {'answer_id': 28394, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not really sure about the <code>System.identityHashCode(Object)</code>. That's pretty much what the <strong>==</strong> is used for. You might rather want to use the <code>Object.hashCode()</code> - it's more in parallel with <code>Object.equals(Object)</code>.</p>\n"}, {'answer_id': 28439, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 1, 'selected': False, 'text': "<p>You should probably raise an exception if it gets to that last <code>return 0</code> line --when a hash collision happens. I do have a question though: you are doing a total ordering on the hash's, which I guess is fine, but shouldn't some function be passed to it to define a Lexicographical order?</p>\n\n<pre><code>    int h1 = System.identityHashCode(o1);\n    int h2 = System.identityHashCode(o2);\n    if (h1 != h2) {\n        return h1 &lt; h2 ? -1 : 1;\n    }\n</code></pre>\n\n<p>I can imagine that you have the objects as a tuple of two integers that form a real number. But you wont get the proper ordering since you're only taking a hash of the object. This is all up to you if hashing is what you meant, but to me, it doesn't make much sense. </p>\n"}, {'answer_id': 28451, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>I agree this is not ideal, hence the comment. Any suggestions?</p>\n</blockquote>\n\n<p>I think there is now way you can solve that, because you cannot access the one and only one thing that can distinguish two instances: their address in memory. So I have only one suggestion: reconsider your need of having a general total ordering process in Java :-)</p>\n'}, {'answer_id': 28477, 'author': 'Cagatay', 'author_id': 3071, 'author_profile': 'https://Stackoverflow.com/users/3071', 'pm_score': 2, 'selected': False, 'text': '<p>Hey, look at what I found!</p>\n\n<p><a href="http://gafter.blogspot.com/2007/03/compact-object-comparator.html" rel="nofollow noreferrer">http://gafter.blogspot.com/2007/03/compact-object-comparator.html</a></p>\n\n<p>This is exactly what I was looking for.</p>\n'}, {'answer_id': 28494, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 3, 'selected': True, 'text': '<blockquote>\n  <p>Hey, look at what I found!</p>\n  \n  <p><a href="http://gafter.blogspot.com/2007/03/compact-object-comparator.html" rel="nofollow noreferrer">http://gafter.blogspot.com/2007/03/compact-object-comparator.html</a></p>\n</blockquote>\n\n<p>Oh yes, I forgot about the IdentityHashMap (Java 6 and above only). Just have to pay attention at releasing your comparator. </p>\n'}]
['https://Stackoverflow.com/questions/28301', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3071/']