How can I sort more data than will fit in memory?
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: see questions 19.27 and 19.30.
References:
Knuth Sec. 5.4 pp. 247-378
Sedgewick Sec. 13 pp. 177-187
Read sequentially: prev next up top
This page by Steve Summit // Copyright 1995 // mail feedback