Collections.sort comparison method violates general contract

First of all, sorry to have asked a question on this topic again. I am well aware that there are many questions and answers here. I have read some of them, but my problem is that I still cannot figure out what I am doing wrong. Here is my 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;
        }

    });

      

I seem to be just blind to see my mistake, so if any of you could help me, I would be very grateful.

EDIT: I want to be able to detect if a line is the top, bottom, leftmost, or rightmost line in a coordinate system that has coordinate 0/0 at the top left corner. Points are of type double. And here is the 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 its general contract!
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.TimSort.mergeHi(TimSort.java:864)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.TimSort.mergeAt(TimSort.java:481)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.TimSort.mergeForceCollapse(TimSort.java:422)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.TimSort.sort(TimSort.java:219)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.TimSort.sort(TimSort.java:169)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.Arrays.sort(Arrays.java:2038)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.util.Collections.sort(Collections.java:1891)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at com.example.camera.RectangleDetector.drawLines(RectangleDetector.java:108)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at com.example.camera.RectangleDetector.findRectangle(RectangleDetector.java:94)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at com.example.camera.MainActivity.onCameraFrame(MainActivity.java:114)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at org.opencv.android.NativeCameraView$CameraWorker.run(NativeCameraView.java:177)
06-03 10:42:30.874: E/AndroidRuntime(15810):    at java.lang.Thread.run(Thread.java:838)

      

+3


source to share


1 answer


This is not the correct order.

Consider the first method:

You return 1 if both y-coordinates of the first (left) point are less than the y-coordinates of the second (right) point, and return -1 otherwise.



This means that if you are comparing a Point whose coordinates are 4 and 6, a point whose coordinates are 6 and 4, you will return -1, no matter which point is the first argument, which is equivalent to saying that Point1 "<" Point2 and Point2 "<" Point1, which should only be possible if Point1 "==" Point2.

Your method compare

must treat each combination of relations and lhs.p1.y rhs.p1.y ( <

, >

, ==

), and the relationship lhs.p2.y and rhs.p2.y ( <

, >

, ==

). those. you must cover 9 conditions of the form (lhs.p1.y rel rhs.p1.y & lhs.p2.y rel rhs.p2.y), where rel <

, >

or ==

.

+5


source







All Articles