I think x.gather(dim, x.argsort(dim)) solves your problem.
import torch
x = torch.Tensor([[3, 1], [2, 4]])
print(x.gather(0, x.argsort(0))) # Columns sorted
print(x.gather(1, x.argsort(1))) # Rows sorted