◆ sort_by_key() [1/4]

template<typename DerivedPolicy , typename RandomAccessIterator1 , typename RandomAccessIterator2 >
__host__ __device__ void thrust::sort_by_key ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
RandomAccessIterator1  keys_first,
RandomAccessIterator1  keys_last,
RandomAccessIterator2  values_first 

sort_by_key performs a key-value sort. That is, sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first)) corresponding to i and j respectively, then *j is not less than *i.

Note: sort_by_key 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 keys or the relative order of their corresponding values will be preserved by sort_by_key.

This version of sort_by_key compares key objects using operator<.

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

execThe execution policy to use for parallelization.
keys_firstThe beginning of the key sequence.
keys_lastThe end of the key sequence.
values_firstThe beginning of the value sequence.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
RandomAccessIterator1is a model of Random Access Iterator, RandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is a model of LessThan Comparable, and the ordering relation on RandomAccessIterator1's value_type is a strict weak ordering, as defined in the LessThan Comparable requirements.
RandomAccessIterator2is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.
The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys using the thrust::host execution policy for parallelization:

#include <thrust/sort.h>
const int N = 6;
int keys[N] = { 1, 4, 2, 8, 5, 7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::sort_by_key(thrust::host, keys, keys + N, values);
// keys is now { 1, 2, 4, 5, 7, 8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}
See also