Now sorts before writing to file.