mindspore.Tensor.sort

Tensor.sort(dim=- 1, descending=False)

Sorts the elements of the self tensor along the given dimension in the specified order.

Warning

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

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

  • descending (bool, optional) – Controls the sort order. If descending is True, the elements are sorted in descending order, or else sorted in ascending order. Default: False .

Returns

  • y1, a tensor whose values are the sorted values, with the same shape and data type as self.

  • y2, a tensor that consists of the indices of the elements in the original self tensor. Data type is int64.

Raises
  • TypeError – If dim is not an int.

  • TypeError – If descending is not a bool.

  • TypeError – If self not in float16, float32, uint8, int8, int16, int32, int64, bfloat16

  • TypeError – If stable is not a bool.

  • ValueError – If dim is not in range of [-len(self.shape), len(self.shape)).

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[8, 2, 1], [5, 9, 3], [4, 6, 7]]), mindspore.float16)
>>> output = x.sort(dim=-1)
>>> # 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=Int64, value=
[[2, 1, 0],
[2, 0, 1],
[0, 1, 2]]))
Tensor.sort(axis=- 1, descending=False)

Sorts the elements of the input tensor along the given dimension in the specified order.

Parameters
  • axis (int, optional) – The dimension to sort along. Default: -1, means the last dimension. The Ascend backend only supports sorting the last dimension.

  • descending (bool, optional) – Controls the sort order. If descending is True, the elements are sorted in descending order, or else sorted in ascending order. Default: False .

Warning

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

Returns

  • y1, a tensor whose values are the sorted values, with the same shape and data type as self.

  • y2, a tensor that consists of the indices of the elements in the original self tensor. Data type is int32.

Raises
  • TypeError – If axis is not an int.

  • TypeError – If descending is not a bool.

  • TypeError – If dtype of self is neither float16, float32, uint8, int8, int16, int32, int64.

  • ValueError – If axis is not in range of [-len(self.shape), len(self.shape)).

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[8, 2, 1], [5, 9, 3], [4, 6, 7]]), mindspore.float16)
>>> output = x.sort(axis=-1)
>>> # 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]]))