This is probably the easiest way
1) read the file line by line and find the first name in your sorting method
e.g.
-read name_1.
-read next name_2.
If name_1 < name_2 then name_2 = name_1 and repeat.
2) read the file line by line again and find the second name.
i.e. The lowest name that is still higher than the first name.
3) write the first name into a file.
4) now read line by line for the third name
5) add the second name into a file
etc...
This will not be fast, but it will have virtual no memory overhead. You will never have more than 3 names stored in memory.