ALL,
std::sort() will sort in ascending order. Is there an easy, convenient and fast way of doing a descending sort?
Thank you.
ALL,
std::sort() will sort in ascending order. Is there an easy, convenient and fast way of doing a descending sort?
Thank you.
If you're sorting int
s, say, in the range [begin, end)
:
std::sort(begin, end, std::greater<int>());
std::greater
is a binary function object that applies >
to its operands. You could alternatively provide a lambda expression:
std::sort(begin, end, [](int a, int b) { return a > b; });
Write a function to compare:
bool comp(int a, int b)
{
return a > b;
}
then to sort, say, a vector vec
, call sort(vec.begin(), vec.end(), comp)