I have a numpy matrix like so:
array([[2, 1, 23, 32], [34, 3, 3, 0], [3, 33, 0, 0], [32, 0, 0, 0]], dtype=int32)
Now
You can also use numpy.argsort with advanced indexing:
numpy.argsort
arr[np.arange(arr.shape[0])[:, None], (arr != 0).argsort(1, kind="mergesort")] #array([[ 2, 1, 23, 32], # [ 0, 34, 3, 3], # [ 0, 0, 3, 33], # [ 0, 0, 0, 32]], dtype=int32)