我有这个代码:
class TimeComparatorTipo0 implements Comparator {
@Override
public int compare(Object a, Object b) {
String Time1 = ((DataImportedTipo0) a).ora;
Long VolTot1 = Long.parseLong(((DataImportedTipo0) a).volume_totale);
String Time2 = ((DataImportedTipo0) b).ora;
Long VolTot2 = Long.parseLong(((DataImportedTipo0) b).volume_totale);
if (Time1.equals(Time2))
{
if ( VolTot1.compareTo(VolTot2) > 0)
return 1;
else
return -1;
}
else
return Time1.compareTo(Time2);
}
};
有时它会抛出这个异常:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:868)
at java.util.TimSort.mergeAt(TimSort.java:485)
at java.util.TimSort.mergeForceCollapse(TimSort.java:426)
at java.util.TimSort.sort(TimSort.java:223)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at ManageUrl.DownloadUrl.StartThreadDowloadTipo0(DownloadUrl.java:893)
at ManageUrl.DownloadUpdateWorkflow$ConsumerTipo0.run(DownloadUpdateWorkflow.java:278)
为什么 ?
1)我怎样才能避免它?2)我怎样才能捕捉到这个异常?
提前致谢。