java collection sort issue -
i use simple comperator , exception , don't know do
this how call:
try { collections.sort(this.closepositions, new positioncomperator()); } catch(exception e) { e.printstacktrace(); }
this comperator:
public class positioncomperator implements comparator<dataresponse> { @override public int compare( dataresponse pos1, dataresponse pos2) { if (pos1.opentime >= pos2.opentime) { return 1; } else { return -1; }// returning 0 merge keys } }
this exception:
java.lang.illegalargumentexception: comparison method violates general contract! @ java.util.timsort.mergelo(unknown source) @ java.util.timsort.mergeat(unknown source) @ java.util.timsort.mergecollapse(unknown source) @ java.util.timsort.sort(unknown source) @ java.util.timsort.sort(unknown source) @ java.util.arrays.sort(unknown source) @ java.util.collections.sort(unknown source) @ gttask.refreshidentityhistory.call(refreshidentityhistory.java:59) @ gttask.refreshidentityhistory.call(refreshidentityhistory.java:1) @ java.util.concurrent.futuretask$sync.innerrun(unknown source) @ java.util.concurrent.futuretask.run(unknown source) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source)
the reason error when sorted 2 items changed order. should include case equal.
preferably do:
return po1.opentime - pos2.opentime;
or do
if (pos1.opentime > pos2.opentime) { return 1; } else if (pos1.opentime < pos2.opentime) { return -1; } else { return 0; }
Comments
Post a Comment