I would use a sort algorithm that works with multiple indices.
For example i would sort based on cx and cy a set of 2d point.
I think to x and y combined in a color and i wish extract the groups of 2d points that have a similar color.
Is the same thing as an ordering based on x and y?