You're on the right track, you have the elements in an array in code. You must have studied the techniques for sorting, heap/ binary, etc.
That would be your next step, to sort the elements from list() to a new aray, slist().
THen write out that array to the new file.