sort¶
- paddle. sort ( x, axis=- 1, descending=False, stable=False, name=None ) [source]
-
Sorts the input along the given axis, and returns the sorted output tensor. The default sort algorithm is ascending, if you want the sort algorithm to be descending, you must set the
descending
as True.- Parameters
-
x (Tensor) – An input N-D Tensor with type float32, float64, int16, int32, int64, uint8.
axis (int, optional) – Axis to compute indices along. The effective range is [-R, R), where R is Rank(x). when axis<0, it works the same way as axis+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.
name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.
- Returns
-
sorted tensor(with the same shape and data type as
x
). - Return type
-
Tensor
Examples
>>> import paddle >>> x = paddle.to_tensor([[[5,8,9,5], ... [0,0,1,7], ... [6,9,2,4]], ... [[5,2,4,2], ... [4,7,7,9], ... [1,7,0,6]]], ... dtype='float32') >>> out1 = paddle.sort(x=x, axis=-1) >>> out2 = paddle.sort(x=x, axis=0) >>> out3 = paddle.sort(x=x, axis=1) >>> print(out1.numpy()) [[[5. 5. 8. 9.] [0. 0. 1. 7.] [2. 4. 6. 9.]] [[2. 2. 4. 5.] [4. 7. 7. 9.] [0. 1. 6. 7.]]] >>> print(out2.numpy()) [[[5. 2. 4. 2.] [0. 0. 1. 7.] [1. 7. 0. 4.]] [[5. 8. 9. 5.] [4. 7. 7. 9.] [6. 9. 2. 6.]]] >>> print(out3.numpy()) [[[0. 0. 1. 4.] [5. 8. 2. 5.] [6. 9. 9. 7.]] [[1. 2. 0. 2.] [4. 7. 4. 6.] [5. 7. 7. 9.]]]