There are several strategies you can use depending on the file size.
For medium size files that almost fit in the available memory:
- load in memory just the sorting keys and the line offsets
- sort this information in memory
- use it to create the final file seeking from line to line into the source file, reading lines following the ordered offsets and writing them to the target file.
For really large files use Sort::External.