megengine.functional.sort

sort(inp, descending=False, dim=- 1)[source]

Returns sorted tensor and the indices would sort the input tensor.

Parameters
  • inp (Tensor) – input tensor. The result would be sorted along the given dimension.

  • descending (bool) – sort in descending order, where the largest comes first. Default: False

  • dim (int) – which dimension to sort along. Default: -1 (last dimension)

Return type

Tuple[Tensor, Tensor]

Returns

tuple of two tensors (sorted_tensor, indices_of_int32).

Examples

>>> import numpy as np
>>> x = Tensor(np.array([1,2], dtype=np.float32))
>>> out, indices = F.sort(x)
>>> out.numpy()
array([1., 2.], dtype=float32)