dpnp.argsort

dpnp.argsort(a, axis=-1, kind=None, order=None, *, stable=None)[source]

Returns the indices that would sort an array.

For full documentation refer to numpy.argsort.

Parameters:
  • a ({dpnp.ndarray, usm_ndarray}) -- Array to be sorted.

  • axis ({None, int}, optional) -- Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.

  • kind ({None, "stable", "mergesort", "radixsort"}, optional) --

    Sorting algorithm. Default is None, which is equivalent to

    "stable".

  • stable ({None, bool}, optional) -- Sort stability. If True, the returned array will maintain the relative order of a values which compare as equal. The same behavior applies when set to False or None. Internally, this option selects kind="stable". Default: None.

Returns:

out -- Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a. More generally, dpnp.take_along_axis(a, index_array, axis=axis) always yields the sorted a, irrespective of dimensionality. The return array has default array index data type.

Return type:

dpnp.ndarray

Notes

For zero-dimensional arrays, if axis=None, output is a one-dimensional array with a single zero element. Otherwise, an AxisError is raised.

Limitations

Parameters order is only supported with its default value. Otherwise NotImplementedError exception will be raised. Sorting algorithms "quicksort" and "heapsort" are not supported.

See also

dpnp.ndarray.argsort

Equivalent method.

dpnp.sort

Return a sorted copy of an array.

dpnp.lexsort

Indirect stable sort with multiple keys.

dpnp.argpartition

Indirect partial sort.

dpnp.take_along_axis

Apply index_array from obj:dpnp.argsort to an array as if by calling sort.

Examples

>>> import dpnp as np
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])
>>> x = np.array([[0, 3], [2, 2]])
>>> x
array([[0, 3],
       [2, 2]])
>>> ind = np.argsort(x, axis=0)  # sorts along first axis
>>> ind
array([[0, 1],
       [1, 0]])
>>> np.take_along_axis(x, ind, axis=0)  # same as np.sort(x, axis=0)
array([[0, 2],
       [2, 3]])
>>> ind = np.argsort(x, axis=1)  # sorts along last axis
>>> ind
array([[0, 1],
       [0, 1]])
>>> np.take_along_axis(x, ind, axis=1)  # same as np.sort(x, axis=1)
array([[0, 3],
       [2, 2]])