Q: How can I sort more data than will fit in memory?
A: 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, and the example in question 19.28.
References:
Knuth Sec. 5.4 pp. 247-378
Sedgewick Sec. 13 pp. 177-187