e.com/w/cpp/ranges-functional-placeholder/less),
                 class Proj = [std::identity](http://en.cppreference.com/w/cpp/utility/functional/identity)>
        requires [std::sortable](http://en.cppreference.com/w/cpp/iterator/sortable)<[ranges::iterator_t](http://en.cppreference.com/w/cpp/ranges-ranges-placeholder/iterator_t)<R>, Comp, Proj>
        constexpr [ranges::borrowed_iterator_t](http://en.cppreference.com/w/cpp/ranges/borrowed_iterator_t)<R>
            operator()(R&& r, Comp comp = {}, Proj proj = {}) const
        {
            return (*this)([ranges::begin](http://en.cppreference.com/w/cpp/ranges-ranges-placeholder/begin)(r), [ranges::end](http://en.cppreference.com/w/cpp/ranges-ranges-placeholder/end)(r), std::move(comp), std::move(proj));
        }
    };
     
    inline constexpr sort_fn sort {};  
  
---  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/algorithm/ranges/sort&action=edit&section=5 "Edit
section: Notes") ]  Notes

[ std::sort ](/w/cpp/algorithm/sort "cpp/algorithm/sort") uses  [
std::iter_swap ](/w/cpp/algorithm/iter_swap "cpp/algorithm/iter swap") to swap
elements, whereas ` ranges::sort ` instead uses  [ ranges::iter_swap
](/w/cpp/iterator/ranges/iter_swap "cpp/iterator/ranges/iter swap") (which
performs ADL for ` iter_swap ` , unlike  [ std::iter_swap
](/w/cpp/algorithm/iter_swap "cpp/algorithm/iter swap") )

###  [ [ edit
](/mwiki/index.php?title=cpp/algorithm/ranges/sort&action=edit&section=6 "Edit
section: Example") ]  Example

Run this code

    
    
    #include <algorithm>
    #include <array>
    #include <functional>
    #include <iomanip>
    #include <iostream>
     
    void print(auto comment, auto const& seq, char term = ' ')
    {
        for ([std::cout](http://en.cppreference.com/w/cpp/io/cout) << comment << '\n'; auto const& elem : seq)
            [std::cout](http://en.cppreference.com/w/cpp/io/cout) << elem << term;
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << '\n';
    }
     
    struct Particle
    {
        [std::string](http://en.cppreference.com/w/cpp/string/basic_string) name; double mass; // MeV
        template<class Os> friend
        Os& operator<<(Os& os, Particle const& p)
        {
            return os << [std::left](http://en.cppreference.com/w/cpp/io/manip/left) << [std::setw](http://en.cppreference.com/w/cpp/io/manip/setw)(8) << p.name << " : " << p.mass << ' ';
        }
    };
     
    int main()
    {
        [std::array](http://en.cppreference.com/w/cpp/container/array) s {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
     
        namespace ranges = std::ranges;
     
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(s);
        print("Sort using the default operator<", s);
     
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(s, [ranges::greater](http://en.cppreference.com/w/cpp/ranges-functional-placeholder/greater)());
        print("Sort using a standard library compare function object", s);
     
        struct
        {
            bool operator()(int a, int b) const { return a < b; }
        } customLess;
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(s.begin(), s.end(), customLess);
        print("Sort using a custom function object", s);
     
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(s, [](int a, int b) { return a > b; });
        print("Sort using a lambda expression", s);
     
        Particle particles[]
        {
            {"Electron", 0.511}, {"Muon", 105.66}, {"Tau", 1776.86},
            {"Positron", 0.511}, {"Proton", 938.27}, {"Neutron", 939.57}
        };
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(particles, {}, &Particle::name);
        print("\nSort by name using a projection", particles, '\n');
        [ranges::sort](http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/sort)(particle