You must use an external sorting algorithm, do not try to put it into memory.
http://en.wikipedia.org/wiki/External_sorting
If you think this is too complicated, try the following:
- include H2 database in your project
- create a new database on the disk (will be created automatically on the first connection)
- create a simple table in which numbers will be saved.
- read data by numbers and insert them into the database (do not forget to fix 1000 numbers or so).
- select numbers with ORDER BY clause :)
- use JDBC resultSet to get results on the fly and write them to the output file
Database
H2 is simple, works fine with Java, and can be built into your JAR (does not require any installation or configuration).
source share