In reply to Ian Wakeling.

Thanks for writing, Ian.

For readers who do not know Ian, he is the King of the One-Liners. Here's what his code does:
1. The sequence x=y; x[rank(x)]=y; is a way to sort an array. See the article, "Rank, order, and sorting."
2. The expression range(M) # row(M) forms a matrix whose rows are constant values. When added to M, the sum has the property that every element in the i_th row is greater than every element in the (i+1)st row, but the elements in each row retain their relative order. For a discussion of the ROW function, see "Filling the lower and upper triangular portions of a matrix."
3. Put together, the RANK function tells you the order of elements in each row. The expression M[ rank(...) ] = Q sorts the matrix by rows.