Template Function thrust::sort(RandomAccessIterator, RandomAccessIterator, StrictWeakOrdering)

Function Documentation

template<typename RandomAccessIterator, typename StrictWeakOrdering>__host__ __device__ void thrust::sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)

sort 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. Note: sort is not guaranteed to be stable. That is, suppose that *i and *j are equivalent: neither one is less than the other. It is not guaranteed that the relative order of these two elements will be preserved by sort.

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

The following code demonstrates how to sort integers in descending order using the greater<int> comparison operator.

Parameters
  • first: The beginning of the sequence.

  • last: The end of the sequence.

  • comp: Comparison operator.

Template Parameters
  • RandomAccessIterator: is 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.

  • StrictWeakOrdering: is a model of Strict Weak Ordering.

#include <thrust/sort.h>
#include <thrust/functional.h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust::sort(A, A + N, thrust::greater<int>());
// A is now {8, 7, 5, 4, 2, 1};

See

http://www.sgi.com/tech/stl/sort.html

See

stable_sort

See

sort_by_key