1. Use list instead of array and use it's built in sort.
2. I mean to say, first put all random numbers in the list, sort them and place them in the file.