◆ stable_sort() [3/4]

template<typename DerivedPolicy , typename RandomAccessIterator , typename StrictWeakOrdering >
__host__ __device__ void thrust::stable_sort ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
RandomAccessIterator  first,
RandomAccessIterator  last,
StrictWeakOrdering  comp 

stable_sort is much like sort: it sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

As the name suggests, stable_sort is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [first, last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort is that x still precedes y.

This version of stable_sort compares objects using a function object comp.

The algorithm's execution is parallelized as determined by exec.

execThe execution policy to use for parallelization.
firstThe beginning of the sequence.
lastThe end of the sequence.
compComparison operator.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
RandomAccessIteratoris a model of Random Access Iterator, RandomAccessIterator is mutable, and RandomAccessIterator's value_type is convertible to StrictWeakOrdering's first_argument_type and second_argument_type.
StrictWeakOrderingis a model of Strict Weak Ordering.

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator using the thrust::host execution policy for parallelization:

#include <thrust/sort.h>
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
// A is now {8, 7, 5, 4, 2, 1};
See also