Skip to content
fn

sort

→_ValuesIndices[Tensor]
sort(input: Tensor, dim: _int = ..., descending: _bool = ..., stable: _bool = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Sort input along dim and return (values, indices).

The sort is stable: equal keys keep their input order.

Parameters

inputTensor
Source tensor.
dimint= -1
Axis to sort along.
descendingbool= False
Largest first.

Returns

_ValuesIndices[Tensor]

Named-tuple-like (values, indices) pair along dim.

Notes

NaN sorts as the largest value: last ascending, first descending. lucid.gather(input, dim, lucid.argsort(input, dim)) equals lucid.sort(input, dim).values. Gradients flow back to the positions the values came from.

Examples

>>> import lucid
>>> x = lucid.tensor([3, 1, 2])
>>> lucid.sort(x, dim=0).values
Tensor([1, 2, 3])
>>> lucid.sort(x, dim=0, descending=True).values
Tensor([3, 2, 1])