When sorting objects with the Collections.sort() method, you will typically want to supply the "comparator" parameter that lets you control the comparison between each object (decide which should come before the other).
For example, if you want to sort by area, I think you can use a static helper method something like the following:
Code:
public static void sortContoursByArea(List<MatOfPoint> contours) {
Collections.sort(contours, new Comparator<MatOfPoint>() {
@Override
public int compare(MatOfPoint a, MatOfPoint b) {
// Not sure how expensive this will be computationally as the
// area is computed for each comparison
double areaA = Imgproc.contourArea(a);
double areaB = Imgproc.contourArea(b);
// Change sign depending on whether your want sorted small to big
// or big to small
if (areaA < areaB) {
return -1;
} else if (areaA > areaB) {
return 1;
}
return 0;
}
});
}
Hope that helps