<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/sort_ops.py#L29-L83) |

Sorts a tensor.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.sort`](https://www.tensorflow.org/api_docs/python/tf/sort)

<br />

    tf.sort(
        values, axis=-1, direction='ASCENDING', name=None
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [TFP Release Notes notebook (0.11.0)](https://www.tensorflow.org/probability/examples/TFP_Release_Notebook_0_11_0) |

#### Usage:

    a = [1, 10, 26.9, 2.8, 166.32, 62.3]
    tf.sort(a).numpy()
    array([  1.  ,   2.8 ,  10.  ,  26.9 ,  62.3 , 166.32], dtype=float32)

    tf.sort(a, direction='DESCENDING').numpy()
    array([166.32,  62.3 ,  26.9 ,  10.  ,   2.8 ,   1.  ], dtype=float32)

For multidimensional inputs you can control which axis the sort is applied
along. The default `axis=-1` sorts the innermost axis.

    mat = [[3,2,1],
           [2,1,3],
           [1,3,2]]
    tf.sort(mat, axis=-1).numpy()
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]], dtype=int32)
    tf.sort(mat, axis=0).numpy()
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 3]], dtype=int32)

<br />

<br />

| ## See also ||
|---|---|
| <br /> - [`tf.argsort`](https://www.tensorflow.org/api_docs/python/tf/argsort): Like sort, but it returns the sort indices. - [`tf.math.top_k`](https://www.tensorflow.org/api_docs/python/tf/math/top_k): A partial sort that returns a fixed number of top values and corresponding indices. ||

<br />

<br />

<br />

<br />

| ## Args ||
|---|---|
| `values` | 1-D or higher **numeric** `Tensor`. |
| `axis` | The axis along which to sort. The default is -1, which sorts the last axis. |
| `direction` | The direction in which to sort the values (`'ASCENDING'` or `'DESCENDING'`). |
| `name` | Optional name for the operation. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `Tensor` with the same dtype and shape as `values`, with the elements sorted along the given `axis`. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| [`tf.errors.InvalidArgumentError`](https://www.tensorflow.org/api_docs/python/tf/errors/InvalidArgumentError) | If the `values.dtype` is not a `float` or `int` type. |
| `ValueError` | If axis is not a constant scalar, or the direction is invalid. |

<br />