mindspore.ops.sort

mindspore.ops.sort(input_x, axis=- 1, descending=False)[source]

Sort the elements of the input tensor along the given axis.

Warning

  • Non-backward-compatible change after version 2.9.0: axis will be renamed to dim, and a new stable parameter will be added.

  • Currently, the data types of float16, uint8, int8, int16, int32, int64 are well supported. If using float32, it may cause loss of accuracy.

Note

The Ascend backend only supports sorting the last dimension.

Parameters
  • input_x (Tensor) – The input tensor.

  • axis (int, optional) – The axis to sort along. Default -1 , means the last dimension.

  • descending (bool, optional) – Sorting method. True means the elements are sorted in descending order, or else sorted in ascending order. Default False .

Returns

Tuple(sorted_tensor, indices) of 2 tensors.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> x = mindspore.tensor([[8, 2, 1], [5, 9, 3], [4, 6, 7]], mindspore.float16)
>>> output = mindspore.ops.sort(x)
>>> # The output below is based on the Ascend platform.
>>> print(output)
(Tensor(shape=[3, 3], dtype=Float16, value=
[[ 1.0000e+00,  2.0000e+00,  8.0000e+00],
[ 3.0000e+00,  5.0000e+00,  9.0000e+00],
[ 4.0000e+00,  6.0000e+00,  7.0000e+00]]), Tensor(shape=[3, 3], dtype=Int32, value=
[[2, 1, 0],
[2, 0, 1],
[0, 1, 2]]))