Take the 2-minute tour ×

I got hit with the (apparently infamous) IllegalArgumentException using Collections.sort() in Java7
Thanks to SO I understand the cause, which is basically (cough) poor code.
The thing is, I cannot reproduce the Exception myself. I did some jdk source code digging, and located which class is throwing that exception. The idea is to create the according Test Case.

Here is the code, by the way
< pride level="0" >

public int compareTo( Symbol other) {
    if( this.lastUse == 0) {
        if( other.lastUse != 0) return (int)( -DateMicros.ONE_DAY);
    } else if( other.lastUse == 0) {
        return ( int)DateMicros.ONE_DAY;
    return ( int)( this.lastUse - other.lastUse);

< /pride > On top of this, "lastUse" gets assigned timestamps in microseconds and milliseconds (yes mixed) which gives a superb int overflow spillage

The actual question is:
What Values Would make This Code Crash? to get a proper test case down the road.

Update with stack trace:

at java.util.ComparableTimSort.mergeHi(Unknown Source)
at java.util.ComparableTimSort.mergeAt(Unknown Source)
at java.util.ComparableTimSort.mergeCollapse(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Hard to tell without looking at the stacktrace itself.

But the docs mentions that the method itself can throw the exception.

(optional) if the implementation detects that the natural ordering of the list elements is found to violate the Comparable contract


It looks like because your object does not fulfull the Comparable consistency requirement:


share|improve this answer
Doh ... Updating with stack trace –  MonoThreaded Oct 23 '12 at 21:47
Does that mean you need to implement equals() consistent with compareTo() to fill the contract? –  MonoThreaded Oct 23 '12 at 22:05
@MonoThreaded: Yes exactly, make your compareTo and equals consistent as described above and see. –  Bhesh Gurung Oct 23 '12 at 22:07

From Java SE 7 and JDK 7 Compatibility

Area: API: Utilities


Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior. Nature of Incompatibility: behavioral

RFE: 6804124

So correct implementation should be like

  public int compareTo( Symbol other) {
        if( this.lastUse == other.lastUse) {
           return 0;
        } else if( other.lastUse>this.lastUse) {
            return 1;
        return -1;


  1. Comparable
share|improve this answer
Once again, I understand the compatibility problem. Thanks though. –  MonoThreaded Oct 23 '12 at 21:53
@MonoThreaded Added correct implementation for your reference –  Amit Deshpande Oct 23 '12 at 21:59
Thanks for the reference. –  MonoThreaded Oct 23 '12 at 22:05

It seems like the crash is caused by the integer overflow, which was not supposed to be however. This was identified as bug , and it gives you the walkaround too.


Try this as temporary fix :

Adding -Djava.util.Arrays.useLegacyMergeSort=true to my eclipse.ini does seem to have resolved the issue.

You can find this fix in the same link I posted though.

Also visit this http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6923200 , which was the same kind of bug reported prior to the first link, however it was closed since they were unable to create the same problem repeatedly.

share|improve this answer
Very nice catch –  MonoThreaded Oct 23 '12 at 22:07
thank you MonoThreaded –  Jimmy Oct 23 '12 at 22:12
That bug is related to this one "swing LayoutComparator breaks transitivity that may lead to IllegalArgumentException". @MonoThreaded: You haven't posted the whole stacktrace? Not able to figure out if yours also is swing related. –  Bhesh Gurung Oct 23 '12 at 22:18
Sorry, it's not –  MonoThreaded Oct 23 '12 at 22:19

Your Answer


