Writing sorted list to file