Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Front Page / Algorithms / Transformation Algorithms / sort



      typename Seq
    , typename Pred = less<_1,_2>
    , typename In = unspecified
struct sort
    typedef unspecified type;


Returns a new sequence of all elements in the range [begin<Seq>::type, end<Seq>::type) sorted according to the ordering relation Pred.

[Note: This wording applies to a no-inserter version(s) of the algorithm. See the Expression semantics subsection for a precise specification of the algorithm's details in all cases — end note]


#include <boost/mpl/sort.hpp>

Model of

Reversible Algorithm


Parameter Requirement Description
Seq Forward Sequence An original sequence.
Pred Binary Lambda Expression An ordering relation.
In Inserter An inserter.

Expression semantics

The semantics of an expression are defined only where they differ from, or are not defined in Reversible Algorithm.

For any Forward Sequence s, a binary Lambda Expression pred, and an Inserter in:

typedef sort<s,pred,in>::type r;
Return type:

A type.


If size<s>::value <= 1, equivalent to

typedef copy<s,in>::type r;

otherwise equivalent to

typedef back_inserter< vector<> > aux_in;
typedef lambda<pred>::type p;

typedef begin<s>::type pivot;
typedef partition<
      iterator_range< next<pivot>::type, end<s>::type >
    , apply_wrap2<p,_1,deref<pivot>::type>
    , aux_in
    , aux_in
    >::type partitioned;

typedef sort<partitioned::first,p,aux_in >::type part1;
typedef sort<partitioned::second,p,aux_in >::type part2;

typedef copy<
          joint_view<part1,single_view< deref<pivot>::type > >
        , part2
    , in
    >::type r;


Average O(n log(n)) where n == size<s>::value, quadratic at worst.


typedef vector_c<int,3,4,0,-5,8,-1,7> numbers;
typedef vector_c<int,-5,-1,0,3,4,7,8> expected;
typedef sort<numbers>::type result;

BOOST_MPL_ASSERT(( equal< result, expected, equal_to<_,_> > ));

See also

Transformation Algorithms, Reversible Algorithm, partition