You can pass a comparator function as the third argument of sort
.
Include algorithm
of course.
#include<algorithm>
Define the comparator function. It should compare two points and return true
if the first one should be before the second one (if first one is smaller than the second one) in the sorted array. The sort
function in <algorithm>
will use this comparator function to compare your items and put them in the right order. You can learn more about sorting algorithms here. If you need more advanced materials you can lookup "Introduction to Algorithms".
bool compare(const point& p1, const point& p2) {
return p1.x < p2.x;
}
Use sort
and pass your array and the function like this:
int main () {
point A[100];
std::sort(A, A+100, compare);
return 0;
}