java - Collections.sort comparison method violates its general contract -


first of all, sorry asking question topic again. i'm aware there plenty of questions , answers here. i've read of them problem still cannot figure out i'm doing wrong. here code:

collections.sort(hlines, new comparator<line>() {          @override         public int compare(line lhs, line rhs) {             if ( lhs.p1.y < rhs.p1.y){                 if (lhs.p2.y < rhs.p2.y)                     return 1;                 else                     return -1;             }             if (lhs.p1.y > rhs.p1.y){                 if (lhs.p2.y > rhs.p2.y)                     return -1;                 else                     return 1;             }             else                 return 0;         }      });     collections.sort(vlines, new comparator<line>() {          @override         public int compare(line lhs, line rhs) {             if ( lhs.p1.x < rhs.p1.x){                 if (lhs.p2.x < rhs.p2.x)                     return 1;                 else                     return -1;             }             if (lhs.p1.x > rhs.p1.x){                 if (lhs.p2.x > rhs.p2.x)                     return -1;                 else                     return 1;             }             else             return 0;         }      }); 

it seems i'm blind see error, if of me out this, thankful.

edit: want determine if line upper, lower, left or moste right line in coordinate system has 0/0 coordinate in upper left corner. points of type double. , here error-message:

06-03 10:42:22.576: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=0 ext2=0 06-03 10:42:22.815: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=1 ext2=0 06-03 10:42:22.848: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=1 ext2=0 06-03 10:42:26.408: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=0 ext2=0 06-03 10:42:26.747: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=1 ext2=0 06-03 10:42:26.781: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=1 ext2=0 06-03 10:42:29.474: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=0 ext2=0 06-03 10:42:30.613: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=0 ext2=0 06-03 10:42:30.646: e/opencv_nativecamera(15810): camerahandler::notify: msgtype=4 ext1=0 ext2=0 06-03 10:42:30.874: e/androidruntime(15810): fatal exception: thread-2592 06-03 10:42:30.874: e/androidruntime(15810): java.lang.illegalargumentexception: comparison method violates general contract! 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.timsort.mergehi(timsort.java:864) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.timsort.mergeat(timsort.java:481) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.timsort.mergeforcecollapse(timsort.java:422) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.timsort.sort(timsort.java:219) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.timsort.sort(timsort.java:169) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.arrays.sort(arrays.java:2038) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.util.collections.sort(collections.java:1891) 06-03 10:42:30.874: e/androidruntime(15810):    @ com.example.camera.rectangledetector.drawlines(rectangledetector.java:108) 06-03 10:42:30.874: e/androidruntime(15810):    @ com.example.camera.rectangledetector.findrectangle(rectangledetector.java:94) 06-03 10:42:30.874: e/androidruntime(15810):    @ com.example.camera.mainactivity.oncameraframe(mainactivity.java:114) 06-03 10:42:30.874: e/androidruntime(15810):    @ org.opencv.android.camerabridgeviewbase.deliveranddrawframe(camerabridgeviewbase.java:387) 06-03 10:42:30.874: e/androidruntime(15810):    @ org.opencv.android.nativecameraview$cameraworker.run(nativecameraview.java:177) 06-03 10:42:30.874: e/androidruntime(15810):    @ java.lang.thread.run(thread.java:838) 

this not proper ordering.

consider first method :

you return 1 if both y coordinates of first (left) point smaller y coordinates of second (right) point, , return -1 otherwise.

this means if comparing point y coords 4 , 6 point y coords 6 , 4, you'll return -1, regardless of point first argument, equivalent saying point1 "<" point2 , point2 "<" point1, should possible if point1 "==" point2.

your compare method should handle each combination of relations of lhs.p1.y , rhs.p1.y (<,>,==) , relations of lhs.p2.y , rhs.p2.y (<,>,==). i.e. have cover 9 conditions of form (lhs.p1.y rel rhs.p1.y && lhs.p2.y rel rhs.p2.y) rel <,> or ==.


Comments