reference.com/w/cpp/ranges-ranges-placeholder/begin) (
r  )  as  first  and  [ ranges::  end
](http://en.cppreference.com/w/cpp/ranges-ranges-placeholder/end) (  r  )  as
last  .

The function-like entities described on this page are _niebloids_ , that is:

  * Explicit template argument lists cannot be specified when calling any of them. 
  * None of them are visible to [ argument-dependent lookup ](/w/cpp/language/adl "cpp/language/adl") . 
  * When any of them are found by [ normal unqualified lookup ](/w/cpp/language/unqualified_lookup "cpp/language/unqualified lookup") as the name to the left of the function-call operator, [ argument-dependent lookup ](/w/cpp/language/adl "cpp/language/adl") is inhibited. 

In practice, they may be implemented as function objects, or with special
compiler extensions.

##  Contents

  * 1  Parameters 
  * 2  Return value 
  * 3  Complexity 
  * 4  Possible implementation 
  * 5  Notes 
  * 6  Example 
  * 7  See also 

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

first, last  |  \-  |  iterator-sentinel defining the range to sort   
---|---|---  
r  |  \-  |  the range to sort   
comp  |  \-  |  comparison to apply to the projected elements   
proj  |  \-  |  projection to apply to the elements   
  
###  [ [ edit
](/mwiki/index.php?title=cpp/algorithm/ranges/sort&action=edit&section=2 "Edit
section: Return value") ]  Return value

An iterator equal to  last  .

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

\\(\scriptsize \mathcal{O}(N\cdot\log{(N)})\\)  𝓞(N·log(N))  comparisons and
projections, where  N  =  [ ranges::  distance
](http://en.cppreference.com/w/cpp/iterator/ranges/distance) (  first, last  )
.

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

Note that typical implementations use [ Introsort
](https://en.wikipedia.org/wiki/Introsort "enwiki:Introsort") . See also the
implementation in [ MSVC STL
](https://github.com/microsoft/STL/blob/e745bad3b1d05b5b19ec652d68abb37865ffa454/stl/inc/algorithm#L7575-L7641)
and [ libstdc++ ](https://github.com/gcc-
mirror/gcc/blob/54258e22b0846aaa6bd3265f592feb161eecda75/libstdc%2B%2B-v3/include/bits/ranges_algo.h#L1808-L1834)
.

    
    
    struct sort_fn
    {
        template<[std::random_access_iterator](http://en.cppreference.com/w/cpp/iterator/random_access_iterator) I, [std::sentinel_for](http://en.cppreference.com/w/cpp/iterator/sentinel_for)<I> S,
                 class Comp = [ranges::less](http://en.cppreference.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)<I, Comp, Proj>
        constexpr I
            operator()(I first, S last, Comp comp = {}, Proj proj = {}) const
        {
            if (first == last)
                return first;
     
            I last_iter = [ranges::next](http://en.cppreference.com/w/cpp/iterator/ranges/next)(first, last);
            [ranges::make_heap](http://en.cppreference.com/w/cpp/algorithm/ranges/make_heap)(first, last_iter, [std::ref](http://en.cppreference.com/w/cpp/utility/functional/ref)(comp), [std::ref](http://en.cppreference.com/w/cpp/utility/functional/ref)(proj));
            [ranges::sort_heap](http://en.cppreference.com/w/cpp/algorithm/ranges/sort_heap)(first, last_iter, [std::ref](http://en.cppreference.com/w/cpp/utility/functional/ref)(comp), [std::ref](http://en.cppreference.com/w/cpp/utility/functional/ref)(proj));
     
            return last_iter;
        }
     
        template<[ranges::random_access_range](http://en.cppreference.com/w/cpp/ranges/random_access_range) R, class Comp = [ranges::less](http://en.cppreferenc