I am trying to reproduce this exception (java.lang.IllegalArgumentException: Comparison method violates its general contract!) as I need to debug a piece of code, but the code below never throws it,
try {
ArrayList al = new ArrayList();
for (int i = 1; i <= 36; i++) {
TypeAdapterSort t = new TypeAdapterSort();
t.order = i;
al.add(t);
}
System.out.println(al.size());
Collections.sort(al, new Comparator() {
public int compare(Object o1, Object o2) {
TypeAdapterSort tas1 = (TypeAdapterSort) o1;
TypeAdapterSort tas2 = (TypeAdapterSort) o2;
if (tas1.order < tas2.order)
return -1;
else
return 1;
}
});
} catch (Exception e) {
System.out.println(e);
}
Also, when I checked the JDK code it seems that this exception is thrown by Collections.sort method only when the size of the collection to be sorted is greater than 32?. What change should be made in the code block so that Collections.sort throws this exception.