First and foremost, instead of writing the file names to a text file and then loading them from the text file back into memory (STL container), just to be able to sort them,
why not keep them in memory (STL container) right away?
Furthermore, instead of using an
std::vector and then having to sort the elements explicitly with
sort(), you could simply use a
std::set, which keeps its elements sorted implicitly/automatically.
(
std::set is usually implemented as a
red–black tree, which allows its elements to be kept sorted extremely efficiently.)
________________
I doubt this is necessary in your use case, but...
If you need to sort
REALLY HUGE data, i.e., data that is too big to load into memory at once, then it is possible to break the list down into separate chunks (files), so that each chunk is small enough to fit into memory. Next, each chunk can be sorted individually, using a normal
in-memory algorithm (e.g. Quicksort). Finally, the sorted chunks (files) can be merged together into a single file using Merge Sort.
That is exactly what the Unix/Linux command-line tool
sort does for very large inputs:
https://en.wikipedia.org/wiki/Sort_(Unix)
________________
Another option would be using an
SQLite "in memory" database to store and sort your data, instead of using C++ STL containers. In my experience, SQLite is much more memory-efficient when it comes to working with
VERY LARGE lists or sets. So, in situations where keeping all data in memory at once is
not possible with a C++ STL container, SQLite may very well still be able to do so!
That plus: With SQLite you can always switch to file-backed database, if neccessary.