# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# C++sort square grid

##### New Coder
Trying to sort 2d coordinates for a square that can be angles relative to x axis. I can get the first corner but I'm having trouble sorting the row.

For exampleThis is sorted but I can get these points in any order and need to sort them like this: 1,3 - 2,4 - 3,5 - 2,2 - 3,3 - 4,4 - 3-1 - 4,2 - 5,3

I can find 1,3 as my starting corner using a bubble sort or sort:

typedef struct
{
float x;
float y;
} points2d;

vector<points2d> pointV;

bool comp(points2d &A, points2d &B)
{
if(A.x > B.x)
{
return 0;
}
else if(A.x == B.x)
{
if(A.y < B.y)
return 0;
}

return 1;
}
sort(mygrid->pointV.begin(), mygrid->pointV.end(), &comp);

I imagine I could find 2,4 - 3,5 by finding the smallest x such that it has a larger value than my starting point of 1,3. Then repeat from the point found. Then I could find row 2 just as I did row 1 by excluding my 3 points and sorting again. Problem is I can't seem to get the points in the row ordered correctly. I keep getting points from another row bubbling up.

1,3 - 2,4 - 2,2 - 3,5
This is where I get stuck 2,2 and 3,5 are out of order in attempts to sort.