I have a strange problem, maybe I'm missing something obvious but I can't fiugre out.
Here is the C++ code that throws the assert:
int compareByX(const Vector2D &a, const Vector2D &b)
{
if (a.x < b.x) // if i put a.x > b.x nothing changes
return -1;
return 1;
}
int main(int argc, char *argv[])
{
double pts[6] = { 5, 34, 3, 54, 10, 34 };
std::vector<Vector2D> points;
for (int i = 0; i < 6; i += 2)
points.push_back({ pts[i],pts[i + 1] });
std::sort(points.begin(), points.end(), compareByX);
}
what happens is that first point (3, 54) is tested against (5, 34), and then viceversa. At that point the assert (invalid comparator) is thrown. But as I see it its right to return -1 as 3 is lesser than 5 and then return 1 since 5 is more than 3...
Can you tell me what is wrong with this?
sort
expects the comparison function to returntrue
(1
) orfalse
(0
). What do you think happens with-1
? What about this way of implementingcompareByX
:{ return a.x < b.x; }
? – Quixotestd::sort
reference should be helpful. – PulverSisVector2D
? – Zima