title: Natural Sort of Directory Filenames in C++
body: <p>I have a directory listing for which I want to retrieve the filenames and put them in a vector of strings such that they are sorted in a "natural" manner. e.g. <code>{ "10.txt" "0.txt" "2.txt" "1.m" "Jan12" "July13.txt" "Nov25.txt" "Jane" "John" }</code> should be <code>{"0.txt" "1.m" "2.txt" "10.txt" "Jan12" "July13.txt" "Nov25.txt" "Jane" "John" }</code> . What is the easiest way to do this?</p>
  
  <p>Elaborating on "natural" we assume for a string made up from parts of numbers <strong>(N)</strong> and text <strong>(T)</strong> such that <code>...(N)(T)...</code> , then for <code>...(N1)(T1)...</code> and  <code>...(N2)(T2)...</code> will be <code>(N1&lt;N2) (&lt;) (T1&lt;T2)</code> where  <code>(&lt;)</code> implies left term precedence over right term. In this case, numbers take precedence over text fields if they are in the same position in the string, i.e. <code>1.z (&lt;) 1_t.txt</code> .</p>
  
  <p>Is there already a library function to do that kind of sort for alphanumeric strings, or directory entries?</p>
  
  <p><strong>DESIRED order</strong> in which files should come. The file names will be stored in a vector of strings.</p>
  
  <pre><code>Abhinav@Abhinav-PC /cygdrive/c/AbhinavSamples/shell
  $ ls -lv
  total 8
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 1.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:55 1_t.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:50 3.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 4.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 10.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 10_t.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 13.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 20.txt
  
  **Simple Sort**
  Abhi@Abhi-PC /cygdrive/c/AbhinavSamples/shell
  $ ls -l
  total 8
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 1.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 10.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 10_t.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:56 13.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:55 1_t.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:53 20.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:50 3.txt
  -rw-r--r--+ 1 Abhinav None 2 Mar 17 00:51 4.txt
  </code></pre>