Partial reverse operation

Hi, I’m trying to implement the following operation:
assume i have a tensor:
[[1,2,3,4,0],
[1,2,3,0,0],
[1,2,0,0,0]]
I want to transform it to:
[[4,3,2,1,0],
[3,2,1,0,0],
[2,1,0,0,0]]

Is there any efficient way to do this?

You could simply sort the values:

x = torch.tensor([[1,2,3,4,0],
                  [1,2,3,0,0],
                  [1,2,0,0,0]])

print(x.sort(dim=1, descending=True).values)
> tensor([[4, 3, 2, 1, 0],
        [3, 2, 1, 0, 0],
        [2, 1, 0, 0, 0]])