Add sorting to the matrix