initial sorting of contours