sort

paddle.compat. sort ( input: Tensor, dim: int = -1, descending: bool = False, stable: bool = False, out=None ) SortRetType [source]

Sorts the input along the given dimension, and returns the sorted output and indices tensor. The default sort algorithm is ascending, if you want the sort algorithm to be descending, you must set the descending as True.

Parameters
  • input (Tensor) – An input N-D Tensor with type float32, float64, int16, int32, int64, uint8, float16, bfloat16

  • dim (int, optional) – Dimension to compute indices along. The effective range is [-R, R), where R is Rank(x). when dim<0, it works the same way as dim+R. Default is -1.

  • descending (bool, optional) – Descending is a flag, if set to true, algorithm will sort by descending order, else sort by ascending order. Default is false.

  • stable (bool, optional) – Whether to use stable sorting algorithm or not. When using stable sorting algorithm, the order of equivalent elements will be preserved. Default is False.

  • out (tuple, optional) – the output tuple/list of (Tensor, Tensor) that can be optionally given to be used as output buffers

Returns

SortRetType, a named tuple which contains values and indices, can be accessed through either indexing (e.g. result[0] for values and result[1] for indices), or by result.values & result.indices

Examples:

>>> import paddle

>>> x = paddle.to_tensor([[5,8,9,5],
...                       [0,0,1,7],
...                       [6,9,2,4]],
...                      dtype='float32')
>>> out1 = paddle.compat.sort(input=x, dim=-1)
>>> out2 = paddle.compat.sort(x, 1, descending=True)
>>> out1
SortRetType(values=Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
       [[5., 5., 8., 9.],
        [0., 0., 1., 7.],
        [2., 4., 6., 9.]]), indices=Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[0, 3, 1, 2],
        [0, 1, 2, 3],
        [2, 3, 0, 1]]))
>>> out2
SortRetType(values=Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
       [[9., 8., 5., 5.],
        [7., 1., 0., 0.],
        [9., 6., 4., 2.]]), indices=Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
       [[2, 1, 0, 3],
        [3, 2, 0, 1],
        [1, 0, 3, 2]]))