You want an ``external sort,'' which you can read about in Knuth, Volume
3. The basic idea is to sort the data in chunks (as much as will fit in memory at
one time), write each sorted chunk to a temporary file, and then merge the files.
Your operating system may provide a general-purpose sort utility, and if so, you
can try invoking it from within your program: