Instead of indexing by hand, one can use the torch.gather 2 function.
import torch 

x = torch.rand(3, 5)
indices = torch.argsort(torch.rand_like(x), dim=-1)
result = torch.gather(x, dim=-1, index=indices)