I have an std::vector of object for which I overloaded the < operator.
How can I use std::sort to sort it in descending order (without needing to write my own Comparator)?
I have an std::vector of object for which I overloaded the < operator.
How can I use std::sort to sort it in descending order (without needing to write my own Comparator)?
You could simply transpose the arguments to std::less with the help of std::bind:
using namespace std::placeholders;
std::sort(v.begin(), v.end(), std::bind(std::less<T>{}, _2, _1));
But I think it'd be much cleaner to simply write the equivalent short lambda, even if it goes against the constraint of not writing your own Comparator:
std::sort(v.begin(), v.end(), [](T const& lhs, T const& rhs) { return rhs < lhs; });
You can sort array by using std::sort and then reverse it with std::reverse. This will sort in your desired way.
std::sort(v.begin(), v.end());
std::reverse(v.begin(), v.end());