1#include <vector>
2#include <algorithm>
3
4using namespace std;
5
6vector< MyStruct > values;
7
8sort( values.begin( ), values.end( ), [ ]( const MyStruct& lhs, const MyStruct& rhs )
9{
10 return lhs.key < rhs.key;
11});
1struct MyStruct
2{
3 int key;
4 std::string stringValue;
5
6 MyStruct(int k, const std::string& s) : key(k), stringValue(s) {}
7};
8
9struct less_than_key
10{
11 inline bool operator() (const MyStruct& struct1, const MyStruct& struct2)
12 {
13 return (struct1.key < struct2.key);
14 }
15};
16
17std::vector < MyStruct > vec;
18
19vec.push_back(MyStruct(4, "test"));
20vec.push_back(MyStruct(3, "a"));
21vec.push_back(MyStruct(2, "is"));
22vec.push_back(MyStruct(1, "this"));
23
24std::sort(vec.begin(), vec.end(), less_than_key());