It is easier if you count the number of zero elements in that dimension
x = torch.randn(5, 7)
x[x<0] = 0
x = x.sort(dim=1)[0] # You forgot that sort returns a pair
first_nonzero = (x == 0).sum(dim=1)

Even easier, you can skip the x[x<0] = 0 line and count the non-positive elements:
x = torch.randn(5, 7)
x = x.sort(dim=1)[0]
first_positive = (x <= 0).sum(dim=1)