Click here to Skip to main content
14,974,268 members
Articles / General Programming / File
Posted 20 Nov 2011

Tagged as


30 bookmarked

Sorting Huge Text Files

Rate me:
Please Sign up or sign in to vote.
4.83/5 (19 votes)
11 Aug 2014CPOL4 min read
Algorithm of sorting lines in text files size of which exceeds available memory


One interviewer asked me how I would sort lines in a 5GB text file, if I had only 1GB of RAM. The task seemed to me quite interesting and I couldn't resist from implementing it the next morning.


I wish I had some background on it, but I really don't know much about external sorting algorithms. I wrote this code mostly using intuition, so I would appreciate if you share your knowledge on more advanced techniques in comments.


In order to sort the file, I first split it to smaller files. On the first iteration, I run through the input file and find all lines that start from the same character. If some resultant files are still larger than the amount of available memory, I split them by first two characters and so on. Then each smaller file is being sorted in memory and saved on the disk. The final stage is merging the sorted list of sorted files into a single file.

Example. Consider the following text file:


For simplicity let's assume that we run a very ancient computer and can't afford sorting files more than 10 bytes long. Our input file is larger, so we need to split it. We start the process by splitting by one character. So after the first step, we will have three files:





Files m and o can now be sorted in memory. However file a is still too large. So we need to split further.




File av is less than ten bytes, however file ap is still large. So we split once again.




Now that we have five small sorted files instead of a single big one, we range them in order of their beginnings and merge them together, saving results into output file.

app apple
apr apricot
av avocado


o orange

Looks good, however this algorithm has a flaw: consider that the input file contains five gigabytes of the same line repeated many times. It's easy to see that in this case the algorithm will be stuck in an endless loop trying to split this file over and over again. A similar problem is illustrated below. Consider we have the following strings and our memory is not sufficient to sort them.


As they all start from 'a', they all will be copied into the same chunk in the first iteration. In the second iteration, we are going to split line by first two characters, however line 'a' consists of only one character! We'll face the same situation in each iteration.

I handle these two problems by separating strings smaller than current substring length into a special unsorted file. As we don't need to sort it, the file can be of any size. (If only case-sensitive sorting were supported, it wouldn't be necessary to save short lines into a file, but only calculate their number.)

Incidentally the algorithm is stable, i.e., it maintains the relative order of records with equal values.

Using the Code

The class HugeFileSort contains the following properties, which specify how sorting will be performed:

  • MaxFileSize - the maximum file size that can be sorted in-memory (100MB by default)
  • StringComparer - comparer used for sorting (case-sensitive CurrentCulture by default)
  • Encoding - the input file encoding (UTF8 by default)

The main method is called simply Sort. It accepts two strings: input and output file names. If size of input file is less than MaxFileSize, its content is simply loaded into memory and being sorted. Otherwise the procedure described above is being performed.

/// <summary>
/// Performs sorting
/// </summary>
/// <param name="inputFileName">The name of the file that needs to be sorted
/// </param>
/// <param name="outputFileName">The name of the file in which 
/// sorted lines from the <paramref name="inputFileName"/> should be saved
/// </param>
public void Sort(string inputFileName, string outputFileName)
    chunks = new SortedDictionary<string, ChunkInfo>(Comparer);
    var info = new FileInfo(inputFileName);
    //If file size is less than maxFileSize simply sort its content in memory.
    if (info.Length < maxFileSize)
        SortFile(inputFileName, outputFileName);
    //Otherwise create temp directory and split file into chunks, 
    //saving each chunk as a new file into this directory
        var dir = new DirectoryInfo("tmp");
        if (dir.Exists)
        SplitFile(inputFileName, 1);

During the execution, temporary directory tmp is created in the current folder. For the sake of better display, final set of temporary files is not deleted and stays in the folder. In production code, please uncomment two lines in the Merge method.

/// <summary>
/// Merges all chunks into one file
/// </summary>
private void Merge(string outputFileName)
    using (var output = File.Create(outputFileName))
        foreach (var name in chunks)
            if (name.Value.NoSortFileName != null)
                CopyFile(name.Value.NoSortFileName, output);
            if (name.Value.FileName != null)
                CopyFile(name.Value.FileName, output);

The core of the algorithm is the splitting method.

/// <summary>
/// Splits big file into some chunks by matching starting characters in each line
/// </summary>
/// <param name="inputFileName">Big file name</param>
/// <param name="chars">Number of starting characters to split by</param>
private void SplitFile(string inputFileName, int chars)
    var files = new Dictionary<string, FileChunk>(Comparer);
    using (var sr = new StreamReader(inputFileName, Encoding))
        while (sr.Peek() >= 0)
            string entry = sr.ReadLine();
            //The length of the line is less than the current 
            //number of characters we split by
            //In this cases we add the line to the non-sorted file
            if (entry.Length < chars)
                ChunkInfo nameInfo;
                if (!chunks.TryGetValue(entry, out nameInfo))
                    chunks.Add(entry, nameInfo = new ChunkInfo());
                nameInfo.AddSmallString(entry, Encoding);
            //Otherwise we add the line to the file corresponding 
            //to the first char characters of the line
                string start = entry.Substring(0, chars);
                FileChunk sfi;
                if (!files.TryGetValue(start, out sfi))
                    sfi = new FileChunk(Encoding);
                    files.Add(start, sfi);
                sfi.Append(entry, Encoding);
    //For each of the chunk we check if size of the chunk is 
    //still greater than the maxFileSize
    foreach (var file in files)
        //If it is - split to smaller chunks
        if (file.Value.Size > maxFileSize)
            SplitFile(file.Value.FileName, chars + 1);
        //Otherwise save it to the dictionary
            SortFile(file.Value.FileName, file.Value.FileName);
            ChunkInfo nameInfo;
            if (!chunks.TryGetValue(file.Key, out nameInfo))
                chunks.Add(file.Key, nameInfo = new ChunkInfo());
            nameInfo.FileName = file.Value.FileName;

FileChunk and ChunkInfo are auxiliary nested classes. The former is the helper that corresponds to new files on each iteration and is used to write lines into files. The latter contains information about the data that will be merged into the resultant file. During the recursive work of the algorithm, the program populates the sorted dictionary that maps text line startings to the instances of ChunkInfo.

SortedDictionary<string, ChunkInfo> chunks;

ChunkInfo contains the following information:

  • FileName - the name of the file that contains sorted lines starting with the given substring
  • NoSortFileName - the name of the file that contains non-sorted lines equal to the given substring (may differ by case)

Both properties can be null.

The class also contains method AddSmallString() that writes a given string into file NoSortFileName.

Test application requires three command-line arguments: input file name, output file name and max file size in bytes. It performs case-insensitive sorting of UTF8 files.


If you want to break it, pass in a large file with little or no line ends. As the algorithm uses standard TextReader to read text line by line from the file, it is not designed to handle such input data.


  • 20th November, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
Canada Canada
No Biography provided

Comments and Discussions

Questionexternal sorting Pin
Member 112003452-Nov-14 8:11
MemberMember 112003452-Nov-14 8:11 
SuggestionNon-streamed data option Pin
Chad3F16-Aug-14 12:34
MemberChad3F16-Aug-14 12:34 
QuestionAnother Alternative Pin
PizzaPie13-Aug-14 6:17
MemberPizzaPie13-Aug-14 6:17 
GeneralRe: Another Alternative Pin
ideafixxxer13-Aug-14 7:57
Memberideafixxxer13-Aug-14 7:57 
GeneralRe: Another Alternative Pin
PizzaPie13-Aug-14 7:58
MemberPizzaPie13-Aug-14 7:58 
GeneralRe: Another Alternative Pin
ideafixxxer13-Aug-14 8:06
Memberideafixxxer13-Aug-14 8:06 
QuestionWindows Sort Pin
Ctznkane52512-Aug-14 3:02
MemberCtznkane52512-Aug-14 3:02 
QuestionNumerical Values? Pin
tmag8-Aug-14 19:14
Membertmag8-Aug-14 19:14 
AnswerRe: Numerical Values? Pin
ideafixxxer11-Aug-14 5:05
Memberideafixxxer11-Aug-14 5:05 
QuestionWhat if? Pin
Petro Protsyk23-Nov-11 1:50
MemberPetro Protsyk23-Nov-11 1:50 
AnswerRe: What if? Pin
ideafixxxer23-Nov-11 5:31
Memberideafixxxer23-Nov-11 5:31 
QuestionI don't understand... Pin
Axel Rietschin21-Nov-11 10:54
professionalAxel Rietschin21-Nov-11 10:54 
AnswerRe: I don't understand... Pin
ideafixxxer22-Nov-11 10:14
Memberideafixxxer22-Nov-11 10:14 
SuggestionRe: I don't understand... Pin
Kevin Bewley12-Aug-14 3:34
MemberKevin Bewley12-Aug-14 3:34 
GeneralRe: I don't understand... Pin
arussell7-Aug-15 11:57
professionalarussell7-Aug-15 11:57 
QuestionPlease show and explain the code. Pin
PIEBALDconsult20-Nov-11 9:40
professionalPIEBALDconsult20-Nov-11 9:40 
AnswerRe: Please show and explain the code. Pin
ideafixxxer20-Nov-11 10:14
Memberideafixxxer20-Nov-11 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.