# Sorting

` template <typename DerivedPolicy, typename RandomAccessIterator> __host__ __device__ void `

**thrust::sort**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator first, RandomAccessIterator last);

template <typename RandomAccessIterator> void **thrust::sort**(RandomAccessIterator first, RandomAccessIterator last);

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

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

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

template <typename RandomAccessIterator> void **thrust::stable_sort**(RandomAccessIterator first, RandomAccessIterator last);

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);

template <typename RandomAccessIterator, typename StrictWeakOrdering> void **thrust::stable_sort**(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);

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);

template <typename RandomAccessIterator1, typename RandomAccessIterator2> void **thrust::sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first);

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

template <typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> void **thrust::sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);

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

template <typename RandomAccessIterator1, typename RandomAccessIterator2> void **thrust::stable_sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first);

template <typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> __host__ __device__ void **thrust::stable_sort_by_key**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);

template <typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> void **thrust::stable_sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);

## Functions

### Function `thrust::sort`

` template <typename DerivedPolicy, typename RandomAccessIterator> __host__ __device__ void `

**sort**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator first, RandomAccessIterator last);`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 `operator<`

.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `sort`

to sort a sequence of integers using the `thrust::host`

execution policy for parallelization:

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

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is a model of LessThan Comparable, and the ordering relation on`RandomAccessIterator's`

`value_type`

is a*strict weak ordering*, as defined in the LessThan Comparable requirements.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the sequence.`first`

The end of the sequence.`last`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort`

`sort_by_key`

### Function `thrust::sort`

` template <typename RandomAccessIterator> void `

**sort**(RandomAccessIterator first, RandomAccessIterator last);`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 `operator<`

.

The following code snippet demonstrates how to use `sort`

to sort a sequence of integers.

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

**Template Parameters**: ** RandomAccessIterator**: is a model of Random Access Iterator,

`RandomAccessIterator`

is mutable, and `RandomAccessIterator's`

`value_type`

is a model of LessThan Comparable, and the ordering relation on `RandomAccessIterator's`

`value_type`

is a *strict weak ordering*, as defined in the LessThan Comparable requirements.

**Function Parameters**:

The beginning of the sequence.`first`

The end of the sequence.`last`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort`

`sort_by_key`

### Function `thrust::sort`

` template <typename DerivedPolicy, typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void `

**sort**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, 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 algorithm’s execution is parallelized as determined by `exec`

.

The following code demonstrates how to sort integers in descending order using the greater`thrust::host`

execution policy for parallelization:

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

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the sequence.`first`

The end of the sequence.`last`

Comparison operator.`comp`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort`

`sort_by_key`

### Function `thrust::sort`

` template <typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void `

**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

```
#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};
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The beginning of the sequence.`first`

The end of the sequence.`last`

Comparison operator.`comp`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort`

`sort_by_key`

### Function `thrust::stable_sort`

` template <typename DerivedPolicy, typename RandomAccessIterator> __host__ __device__ void `

**stable_sort**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator first, RandomAccessIterator last);`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 `operator<`

.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `sort`

to sort a sequence of integers using the `thrust::host`

execution policy for parallelization:

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

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is a model of LessThan Comparable, and the ordering relation on`RandomAccessIterator's`

`value_type`

is a*strict weak ordering*, as defined in the LessThan Comparable requirements.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the sequence.`first`

The end of the sequence.`last`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/stable_sort
`sort`

`stable_sort_by_key`

### Function `thrust::stable_sort`

` template <typename RandomAccessIterator> void `

**stable_sort**(RandomAccessIterator first, RandomAccessIterator last);`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 `operator<`

.

The following code snippet demonstrates how to use `sort`

to sort a sequence of integers.

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

**Template Parameters**: ** RandomAccessIterator**: is a model of Random Access Iterator,

`RandomAccessIterator`

is mutable, and `RandomAccessIterator's`

`value_type`

is a model of LessThan Comparable, and the ordering relation on `RandomAccessIterator's`

`value_type`

is a *strict weak ordering*, as defined in the LessThan Comparable requirements.

**Function Parameters**:

The beginning of the sequence.`first`

The end of the sequence.`last`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/stable_sort
`sort`

`stable_sort_by_key`

### Function `thrust::stable_sort`

` template <typename DerivedPolicy, typename RandomAccessIterator, typename StrictWeakOrdering> __host__ __device__ void `

**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`

.

The following code demonstrates how to sort integers in descending order using the greater`thrust::host`

execution policy for parallelization:

```
#include <thrust/sort.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.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};
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the sequence.`first`

The end of the sequence.`last`

Comparison operator.`comp`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/stable_sort
`sort`

`stable_sort_by_key`

### Function `thrust::stable_sort`

` template <typename RandomAccessIterator, typename StrictWeakOrdering> void `

**stable_sort**(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`

.

`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 following code demonstrates how to sort integers in descending order using the greater

```
#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};
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator`

`RandomAccessIterator`

is mutable, and`RandomAccessIterator's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The beginning of the sequence.`first`

The end of the sequence.`last`

Comparison operator.`comp`

**See**:

- https://en.cppreference.com/w/cpp/algorithm/stable_sort
`sort`

`stable_sort_by_key`

### Function `thrust::sort_by_key`

` template <typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2> __host__ __device__ void `

**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`

.

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>
#include <thrust/execution_policy.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'}
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator1`

`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.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort_by_key`

`sort`

### Function `thrust::sort_by_key`

` template <typename RandomAccessIterator1, typename RandomAccessIterator2> void `

**sort_by_key**(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 following code snippet demonstrates how to use `sort_by_key`

to sort an array of character values using integers as sorting keys.

```
#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(keys, keys + N, values);
// keys is now { 1, 2, 4, 5, 7, 8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator1`

`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.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.

**Function Parameters**:

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort_by_key`

`sort`

### Function `thrust::sort_by_key`

` template <typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> __host__ __device__ void `

**sort_by_key**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);`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 a function object `comp`

.

The algorithm’s execution is parallelized as determined by `exec`

.

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.The keys are sorted in descending order using the `greater<int>`

comparison operator.

```
#include <thrust/sort.h>
#include <thrust/execution_policy.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, thrust::greater<int>());
// keys is now { 8, 7, 5, 4, 2, 1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator1`

`RandomAccessIterator1`

is mutable, and`RandomAccessIterator1's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

Comparison operator.`comp`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort_by_key`

`sort`

### Function `thrust::sort_by_key`

` template <typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> void `

**sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);`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`

.

`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 a function object `comp`

.

The following code snippet demonstrates how to use `sort_by_key`

to sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater

```
#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(keys, keys + N, values, thrust::greater<int>());
// keys is now { 8, 7, 5, 4, 2, 1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator1`

`RandomAccessIterator1`

is mutable, and`RandomAccessIterator1's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

Comparison operator.`comp`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`stable_sort_by_key`

`sort`

### Function `thrust::stable_sort_by_key`

` template <typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2> __host__ __device__ void `

**stable_sort_by_key**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first);`stable_sort_by_key`

performs a key-value sort. That is, `stable_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`

.

As the name suggests, `stable_sort_by_key`

is stable: it preserves the relative ordering of equivalent elements. That is, if `x`

and `y`

are elements in `[keys_first, keys_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_by_key`

is that `x`

still precedes `y`

.

This version of `stable_sort_by_key`

compares key objects using `operator<`

.

The algorithm’s execution is parallelized as determined by `exec`

.

The following code snippet demonstrates how to use `stable_sort_by_key`

to sort an array of characters using integers as sorting keys using the `thrust::host`

execution policy for parallelization:

```
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
...
const int N = 6;
int keys[N] = { 1, 4, 2, 8, 5, 7};
char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust::stable_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'}
```

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator1`

`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.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`sort_by_key`

`stable_sort`

### Function `thrust::stable_sort_by_key`

` template <typename RandomAccessIterator1, typename RandomAccessIterator2> void `

**stable_sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first);`stable_sort_by_key`

performs a key-value sort. That is, `stable_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`

.

As the name suggests, `stable_sort_by_key`

is stable: it preserves the relative ordering of equivalent elements. That is, if `x`

and `y`

are elements in `[keys_first, keys_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_by_key`

is that `x`

still precedes `y`

.

This version of `stable_sort_by_key`

compares key objects using `operator<`

.

The following code snippet demonstrates how to use `stable_sort_by_key`

to sort an array of characters using integers as sorting keys.

```
#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::stable_sort_by_key(keys, keys + N, values);
// keys is now { 1, 2, 4, 5, 7, 8}
// values is now {'a', 'c', 'b', 'e', 'f', 'd'}
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator1`

`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.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.

**Function Parameters**:

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`sort_by_key`

`stable_sort`

### Function `thrust::stable_sort_by_key`

` template <typename DerivedPolicy, typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> __host__ __device__ void `

**stable_sort_by_key**(const thrust::detail::execution_policy_base< DerivedPolicy > & exec, RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);`stable_sort_by_key`

performs a key-value sort. That is, `stable_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`

.

As the name suggests, `stable_sort_by_key`

is stable: it preserves the relative ordering of equivalent elements. That is, if `x`

and `y`

are elements in `[keys_first, keys_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_by_key`

is that `x`

still precedes `y`

.

This version of `stable_sort_by_key`

compares key objects using the function object `comp`

.

The algorithm’s execution is parallelized as determined by `exec`

.

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. The keys are sorted in descending order using the `greater<int>`

comparison operator.

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

**Template Parameters**:

The name of the derived execution policy.`DerivedPolicy`

is a model of Random Access Iterator,`RandomAccessIterator1`

`RandomAccessIterator1`

is mutable, and`RandomAccessIterator1's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The execution policy to use for parallelization.`exec`

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

Comparison operator.`comp`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`sort_by_key`

`stable_sort`

### Function `thrust::stable_sort_by_key`

` template <typename RandomAccessIterator1, typename RandomAccessIterator2, typename StrictWeakOrdering> void `

**stable_sort_by_key**(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last, RandomAccessIterator2 values_first, StrictWeakOrdering comp);`stable_sort_by_key`

performs a key-value sort. That is, `stable_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`

.

`stable_sort_by_key`

is stable: it preserves the relative ordering of equivalent elements. That is, if `x`

and `y`

are elements in `[keys_first, keys_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_by_key`

is that `x`

still precedes `y`

.

This version of `stable_sort_by_key`

compares key objects using the function object `comp`

.

The following code snippet demonstrates how to use `sort_by_key`

to sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater

```
#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::stable_sort_by_key(keys, keys + N, values, thrust::greater<int>());
// keys is now { 8, 7, 5, 4, 2, 1}
// values is now {'d', 'f', 'e', 'b', 'c', 'a'}
```

**Template Parameters**:

is a model of Random Access Iterator,`RandomAccessIterator1`

`RandomAccessIterator1`

is mutable, and`RandomAccessIterator1's`

`value_type`

is convertible to`StrictWeakOrdering's`

`first_argument_type`

and`second_argument_type`

.is a model of Random Access Iterator, and`RandomAccessIterator2`

`RandomAccessIterator2`

is mutable.is a model of Strict Weak Ordering.`StrictWeakOrdering`

**Function Parameters**:

The beginning of the key sequence.`keys_first`

The end of the key sequence.`keys_last`

The beginning of the value sequence.`values_first`

Comparison operator.`comp`

**Preconditions**: The range `[keys_first, keys_last))`

shall not overlap the range `[values_first, values_first + (keys_last - keys_first))`

.

**See**:

- https://en.cppreference.com/w/cpp/algorithm/sort
`sort_by_key`

`stable_sort`