Problem Statement:
I want to sort an std::vector of a structure using my custom sorting criteria.
The structure is:
struct Node
{
int x;
int y;
float value;
};
The vector is:
std::vector<Node> vec;
My custom sorting criteria is that the vector should first be sorted by y and then by x (just like in Microsoft Excel).
Example:
Input:
x y
5 6
2 4
1 1
1 0
8 10
4 7
7 1
5 4
6 1
1 4
3 10
7 2
Output:
x y
1 0
1 1
6 1
7 1
7 2
1 4
2 4
5 4
5 6
4 7
3 10
8 10
Can the above mentioned sorting be achieved through any of the C++ Standard Library sorting functions? If not, is there any other library which I can use?
Yes, you can do it using
std::sortusing a comparison function.