sort(), argsort() and msort() in PyTorch
Tags: pytorch, sort, argsort, msort
Published: 2024-04-19 23:29:17

[sort()](https://pytorch.org/docs/stable/generated/torch.sort.html) can get two of the 0D or more D tensors of zero or more sorted elements and their indices in ascending(Default) or descending order from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `sort()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) and a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `bool`). *The 0D tensor of a complex number can be used.
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Defualt:`-1`-Type:`int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `descending`(Optional-Default:`False`-Type:`bool`). *`False` is ascending order and `True` is descending order.
- The 4th argument with `torch` or the 3rd argument with a tensor is `stable`(Optional-Default:`False`-Type:`bool`). 
  *Memos:
  - `True` can definitely sort the multiple same values while `False` cannot but even `False` basically can sort the multiple same values.
  - You must use `stable=`. *Setting `stable`, `dim` and `descending` also needs `dim=` and `descending=` respectively.
- There is `out` argument with `torch`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.sort(my_tensor)
my_tensor.sort()
torch.sort(input=my_tensor, dim=0, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=0, descending=False, stable=True)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([-5, -3, -3, 0, 1, 7, 7, 9]),
# indices=tensor([2, 5, 7, 6, 1, 0, 3, 4]))

torch.sort(input=my_tensor, dim=0, descending=True, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=True, stable=False)
# torch.return_types.sort(
# values=tensor([9, 7, 7, 1, 0, -3, -3, -5]),
# indices=tensor([4, 0, 3, 1, 6, 5, 7, 2]))

my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])

torch.sort(input=my_tensor)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=1, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-1, descending=False, stable=True)
torch.sort(input=my_tensor, dim=1, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([[-5, 1, 7, 7], [-3, -3, 0, 9]]),
# indices=tensor([[2, 1, 0, 3], [1, 3, 2, 0]]))

torch.sort(input=my_tensor, dim=0, descending=False, stable=False)
torch.sort(input=my_tensor, dim=-2, descending=False, stable=False)
torch.sort(input=my_tensor, dim=0, descending=False, stable=True)
torch.sort(input=my_tensor, dim=-2, descending=False, stable=True)
# torch.return_types.sort(
# values=tensor([[7, -3, -5, -3], [9, 1, 0, 7]]),
# indices=tensor([[0, 1, 0, 1], [1, 0, 1, 0]]))

my_tensor = torch.tensor([[7., 1, -5., 7.], [9., -3., 0, -3.]])

torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor([[-5., 1., 7., 7.], [-3., -3., 0., 9.]]),
# indices=tensor([[2, 1, 0, 3], [1, 3, 2, 0]]))

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor([[False, False, True,  True],
#                [False, False, True,  True]]),
# indices=tensor([[1, 3, 0, 2],
#                 [0, 2, 1, 3]]))

my_tensor = torch.tensor(7.+0.j)

torch.sort(input=my_tensor)
# torch.return_types.sort(
# values=tensor(7.+0.j),
# indices=tensor(0))
```

[argsort()](https://pytorch.org/docs/stable/generated/torch.argsort.html) can get the 0D or more D tensor of zero or more sorted elements' indices in ascending(Default) or descending order from the 0D or more D tensor of  zero or more elements as shown below:

*Memos:
- `argsort()` can be used with `torch` and a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `bool`). *The 0D tensor of a complex number can be used.
- The 2nd argument with `torch` or the 1st argument with a tensor is `dim`(Optional-Defualt:`-1`Type:`int`).
- The 3rd argument with `torch` or the 2nd argument with a tensor is `descending`(Optional-Default:`False`-Type:`bool`). *`False` is ascending order and `True` is descending order.
- The 4th argument with `torch` or the 3rd argument with a tensor is `stable`(Optional-Default:`False`-Type:`bool`). 
  *Memos:
  - `True` can definitely sort the multiple same values while `False` cannot but even `False` can basically sort the multiple same values.
  - You must use `stable=`. *When setting `stable`, `dim` and `descending` also need `dim=` and `descending=` respectively.

```python
import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.argsort(input=my_tensor)
my_tensor.argsort()
torch.argsort(input=my_tensor, dim=0, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=0, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=True)
# tensor([2, 5, 7, 6, 1, 0, 3, 4])

torch.argsort(input=my_tensor, dim=0, descending=True, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=True, stable=False)
# tensor([4, 0, 3, 1, 6, 5, 7, 2])

my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])

torch.argsort(input=my_tensor)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=1, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-1, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=1, descending=False, stable=True)
# tensor([[2, 1, 0, 3], [1, 3, 2, 0]])

torch.argsort(input=my_tensor, dim=0, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=-2, descending=False, stable=False)
torch.argsort(input=my_tensor, dim=0, descending=False, stable=True)
torch.argsort(input=my_tensor, dim=-2, descending=False, stable=True)
# tensor([[0, 1, 0, 1], [1, 0, 1, 0]])

my_tensor = torch.tensor([[7., 1., -5., 7.], [9., -3., 0., -3.]])

torch.argsort(input=my_tensor)
# tensor([[2, 1, 0, 3], [1, 3, 2, 0]])

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.argsort(input=my_tensor)
# tensor([[1, 3, 0, 2],
#         [0, 2, 1, 3]])

my_tensor = torch.tensor(7.+0.j)

torch.argsort(input=my_tensor)
# tensor(0)
```

[msort()](https://pytorch.org/docs/stable/generated/torch.msort.html) can get the 0D or more D tensor of zero or more sorted elements in ascending order from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `msort()` can be used with `torch` and a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `bool`). *The 0D tensor of a complex number can be used.
- `msort()` doesn't have `dim`, `descending` and `stable` argument.
- `msort()` is equivalent to `sort()[0]` with `dim=0`.
- There is `out` argument with `torch`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.msort(input=my_tensor)
my_tensor.msort()
# tensor([-5, -3, -3, 0, 1, 7, 7, 9])

my_tensor = torch.tensor([[7, 1, -5, 7], [9, -3, 0, -3]])

torch.msort(input=my_tensor)
# tensor([[7, -3, -5, -3], [9, 1, 0, 7]])

my_tensor = torch.tensor([[7., 1., -5., 7.], [9., -3., 0., -3.]])

torch.msort(input=my_tensor)
# tensor([[7., -3., -5., -3.], [9., 1., 0., 7.]])

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True]])
torch.msort(input=my_tensor)
# tensor([[False, False, False, False],
#         [True, True, True, True]])

my_tensor = torch.tensor(7.+5.j)

torch.msort(input=my_tensor)
# tensor(7.+5.j)
```