te_random  ](/w/cpp/algorithm/ranges/generate_random
"cpp/algorithm/ranges/generate random") (C++26)  
[ Return types ](/w/cpp/algorithm/ranges#Return_types "cpp/algorithm/ranges")  
|  [ ranges::in_fun_result
](/w/cpp/algorithm/ranges/return_types/in_fun_result
"cpp/algorithm/ranges/return types/in fun result")  
---  
[ ranges::in_in_result  ](/w/cpp/algorithm/ranges/return_types/in_in_result
"cpp/algorithm/ranges/return types/in in result")  
[ ranges::in_found_result
](/w/cpp/algorithm/ranges/return_types/in_found_result
"cpp/algorithm/ranges/return types/in found result")  
  
|  [ ranges::in_out_result
](/w/cpp/algorithm/ranges/return_types/in_out_result
"cpp/algorithm/ranges/return types/in out result")  
---  
[ ranges::in_in_out_result
](/w/cpp/algorithm/ranges/return_types/in_in_out_result
"cpp/algorithm/ranges/return types/in in out result")  
  
  
  
|  [ ranges::in_out_out_result
](/w/cpp/algorithm/ranges/return_types/in_out_out_result
"cpp/algorithm/ranges/return types/in out out result")  
---  
[ ranges::min_max_result
](/w/cpp/algorithm/ranges/return_types/min_max_result
"cpp/algorithm/ranges/return types/min max result")  
  
  
  
|  [ ranges::out_value_result
](/w/cpp/algorithm/ranges/return_types/out_value_result
"cpp/algorithm/ranges/return types/out value result") (C++23)  
---  
[ ranges::in_value_result
](/w/cpp/algorithm/ranges/return_types/in_value_result
"cpp/algorithm/ranges/return types/in value result") (C++23)  
  
  
  
[ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/algorithm/ranges/navbar_content&action=edit)

Defined in header ` [ <algorithm> ](/w/cpp/header/algorithm "cpp/header/algorithm") ` |  |   
---|---|---  
Call signature  |  |   
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  
sort  (  I first, S last, Comp comp  =  {  }  , Proj proj  =  {  }  )  ;  |  (1)  |  (since C++20)   
template  < [ ranges::  random_access_range
](http://en.cppreference.com/w/cpp/ranges/random_access_range) R,  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) < [ 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  >  
sort  (  R  && r, Comp comp  =  {  }  , Proj proj  =  {  }  )  ;  |  (2)  |  (since C++20)   
|  |   
  
Sorts the elements in the range ` [ ` first  ` , ` last  ` ) ` in non-
descending order. The order of equivalent elements is not guaranteed to be
preserved.

A sequence is sorted with respect to a comparator  comp  if for any iterator `
it ` pointing to the sequence and any non-negative integer ` n ` such that `
it + n ` is a valid iterator pointing to an element of the sequence,  [ std::
invoke  ](http://en.cppreference.com/w/cpp/utility/functional/invoke) (  comp,
[ std::  invoke  ](http://en.cppreference.com/w/cpp/utility/functional/invoke)
(  proj,  *  (  it  \+  n  )  )  , [ std::  invoke
](http://en.cppreference.com/w/cpp/utility/functional/invoke) (  proj,  *  it
)  )  evaluates to  false  .

1)  Elements are compared using the given binary comparison function  comp  .

2)  Same as  (1)  , but uses  r  as the source range, as if using  [ ranges::
begin  ](http://en.cpp