sort matrix entries