Sorting an object by a member

1
2
3
4
5
6
7
8
9
class test{
    int position, value;
  public:
    test (int a, int b){ position = a; value = b;
}
//...
vector <test> o;
//read some values
//sort the class by value 

I don't how to sort the class.
i.e.:
1 4
2 1
5 2

should be
2 1
5 2
1 4
Last edited on
Take a look at http://www.cplusplus.com/reference/algorithm/sort/
If I understand you corectly, you may want to write a comparision function.
something like bool cmp(test a, test b){ return a.value < b.value; }
Hope this helps
Thanks! It works.
Topic archived. No new replies allowed.