'OpenCV C++ Sorting a vector of contour point in decending order

GETTING THIS ERROR MESSAGES 1

GETTING THIS ERROR MESSAGES 2

I'm trying to sort vector of contours point in decending order but whenever I used:

sort(contours.begin(), contours.end() , greater<>()),

It is poping out an error.
How to sort vector that contains contour points in decending order?

    Mat img;
    im.copyTo(img);
    vector<vector<Point>>contours;
    vector<Vec4i>hierachy;
    findContours(Canny_img , contours , hierachy , RETR_EXTERNAL , CHAIN_APPROX_SIMPLE);
    sort(contours.begin(), contours.end() , greater<Point>()); //This line getting error
   
    for(int i = 0; i < contours.size(); i++)
    {
        drawContours(img , contours , i , Scalar(0,255,0) , 2);
        imshow("Contour Image" , img);
        waitKey(0);
    }


Solution 1:[1]

contours is not a vector of Points. It is a vector of vectors of Points. I.e. each element is in itself a vector of Points.

If you want to sort such a vector of vectors, you should supply some kind of a "greater" function.

One of the convenient ways would be using a lamba function:

sort(contours.begin(), 
    contours.end(), 
    [](std::vector<cv::Point> const & v1, std::vector<cv::Point> const & v2) { return true; });

As you can see the lambda simply returns true. This is just a stub. Instead you should implement the criterion you want for sorting.

You have to determine when you consider one element (which is a vector of Points) to be greater than another (also a vector of Points). It depends on what you want to actualy do with the contours.

Note: it is better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?. In my opinion it's better to avoid using namespace cv as well, from a similar reason.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1