the second output of sort is the indices i the original ordering.
You can use these indices and a scatter_
operation to unsort
to the original permutation.
x = torch.randn(10)
y, ind = torch.sort(x, 0)
unsorted = y.new(*y.size())
unsorted.scatter_(0, ind, y)
print((x - unsorted).abs().max())