The rows of the matrix M could be sorted in one go using something like:

Q = M ;
M[ rank(M + range(M) # row(M)) ] = Q ;

It blows up if there are any missing values, but in the context of a simulation you may know that you don't have any.