Two function calls DO NOT give the same answer, because std::sort not a stable algorithm , i.e. he does not preserve the same elements in his relative orders. Below is an example where the elements std::pair<int, int> are sorted by their first element. Sorting and sorting in the reverse order using the inverse comparison function does not produce identical sequences. The same with std::stable_sort gives the same results.
#include <algorithm> #include <iostream> #include <ios> #include <vector> int main() { typedef std::pair<int, int> Element; std::vector<Element> v; v.push_back( Element(1,1) ); v.push_back( Element(-1,1) ); v.push_back( Element(1,2) ); v.push_back( Element(-1,2) ); v.push_back( Element(1,3) ); v.push_back( Element(-1,3) ); v.push_back( Element(1,4) ); v.push_back( Element(-1,4) ); v.push_back( Element(1,5) ); v.push_back( Element(-1,5) ); v.push_back( Element(1,6) ); v.push_back( Element(-1,6) ); v.push_back( Element(1,16) ); v.push_back( Element(-1,16) ); v.push_back( Element(1,22) ); v.push_back( Element(-1,22) ); v.push_back( Element(1,33) ); v.push_back( Element(-1,33) ); v.push_back( Element(1,44) ); v.push_back( Element(-1,44) ); v.push_back( Element(1,55) ); v.push_back( Element(-1,55) ); v.push_back( Element(1,66) ); v.push_back( Element(-1,66) ); for (auto it = v.begin(); it != v.end(); ++it) { std::cout << "(" << it->first << "," << it->second << ")" << " "; } std::cout << "\n"; auto w1 = v; std::sort(w1.begin(), w1.end(), [](Element const& e1, Element const& e2){ return e1.first < e2. first; }); auto w2 = v; std::sort(w2.rbegin(), w2.rend(), [](Element const& e1, Element const& e2) { return e1.first > e2.first; }); std::cout << std::boolalpha << std::equal(w1.begin(), w1.end(), w2.begin()) << "\n"; auto w3 = v; std::stable_sort(w3.begin(), w3.end(), [](Element const& e1, Element const& e2){ return e1.first < e2. first; }); auto w4 = v; std::stable_sort(w4.rbegin(), w4.rend(), [](Element const& e1, Element const& e2) { return e1.first > e2.first; }); std::cout << std::boolalpha << std::equal(w3.begin(), w3.end(), w4.begin()) << "\n"; }
Output to LiveWorkSpace