Sorting elements of vector where each element is a pair

c++, sorting, vector

Solution

struct cmp_by_first {
  template<typename T>
  bool operator<(const T& x, const T& y) const { return x.first < y.first; }
};

std::sort(vect.begin(), vect.end(), cmp_by_first());

Problem

Possible Duplicate: How do I sort a vector of pairs based on the second element of the pair? I have a vector of this type: `vector< pair<float, int> > vect;` I want sort its elements according to the descending order of the floats values (the first value of pairs). For example `vect = [<8.6, 4>, <5.2, 9>, <7.1, 23>]`, after sorting I want to have: `[<5.2, 9>, <7.1, 23>, <8.6, 4>]` how can I simply do that in C++ ?

Original source

Related problems