# Sort #### 2022-06-11 --- ##### Algorithms: #algorithm #sort ##### Difficulty: #CS_analysis #difficulty_easy ##### Related problems: ##### Links: - [cpp reference](https://en.cppreference.com/w/cpp/algorithm/sort) --- ### What is Sort? Sorts the elements in the range `[first, last)` in non-descending order. The order of equal elements is not guaranteed to be preserved. The default Compare function is less: `<` ### Example ```cpp #include #include #include #include #include int main() { std::array s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; auto print = [&s](std::string_view const rem) { for (auto a : s) { std::cout << a << ' '; } std::cout << ": " << rem << '\n'; }; std::sort(s.begin(), s.end()); print("sorted with the default operator<"); std::sort(s.begin(), s.end(), std::greater()); print("sorted with the standard library compare function object"); struct { bool operator()(int a, int b) const { return a < b; } } customLess; std::sort(s.begin(), s.end(), customLess); print("sorted with a custom function object"); std::sort(s.begin(), s.end(), [](int a, int b) { return a > b; }); print("sorted with a lambda expression"); } ```