I am trying to sort a vector of pointers to a class A using std::sort, but am having some difficulties.
Imagine class A beings just a point, containing x and y coordinates. I want to sort the vector by y coordinates from biggest to lowest using some fixed offset value. And on top of this I want to sort it by x coordinatex, from lowest to biggest. I've had something like following in mind but as you can imagine its not working as wanted:
bool A::sortByCoordinates(const A *a, const A *b)
{
if ((b->y < a->y - offset) || (b->y > a->y + offset)) {
return false;
} else if (b->x < a->x) {
return true;
}
return false;
}
void A::recalculate(std::vector<A *> &test)
{
std::sort(test.begin(), test.end(), sortByCoordinates);
}
In short, if b->y < a->y - offset or b->y > a->y + offset treat it as b->y == a->y and then sort by their x coordinates from lowest to biggest. If the above is not true sort it as b->y < a->y.
How can I achieve this?
EDIT:
Imagine a xy plane such as this:
where black dots represent class A with x and y coordinates. I want to split this plane into finitely many sections which are represented by the red lines and are wide as offset. Now I want to treat points in these sections as tho they had the same y coordinate and sort them only by their x coordinate.