title: How to sort files numerically from linux command line
body: <p>Okay, now this is more a rant about Linux than a question, but maybe someone knows how to do what I want. I know this <em>can</em> be <em>achieved</em> using the <code>sort</code> command, but I want a better solution because getting that to work is about as easy as writing a C program to do the same thing.</p>
  
  <p>I have files, for arguments sake, lets say I have these files: (my files are the same I just have many more)</p>
  
  <ul>
  <li>file-10.xml</li>
  <li>file-20.xml</li>
  <li>file-100.xml</li>
  <li>file-k10.xml</li>
  <li>file-k20.xml</li>
  <li>file-k100.xml</li>
  <li>file-M10.xml</li>
  <li>file-M20.xml</li>
  <li>file-M100.xml</li>
  </ul>
  
  <p>Now this turns out to be the order I want them sorted in. Incidentally, this is the order <em>in Windows</em> that they are by default sorted into. That's nice. Windows groups consecutive numerical characters into <em>one effective character</em> which sorts alphabetically before letters.</p>
  
  <p>If I type <code>ls</code> at the linux command line, I get the following garbage. Notice the 20 is displaced. This is a bigger deal when I have hundreds of these files that I want to view in a report, in order.</p>
  
  <ul>
  <li>file-100.xml</li>
  <li>file-10.xml</li>
  <li>file-20.xml</li>
  <li>file-k100.xml</li>
  <li>file-k10.xml</li>
  <li>file-k20.xml</li>
  <li>file-M100.xml</li>
  <li>file-M10.xml</li>
  <li>file-M20.xml</li>
  </ul>
  
  <p>I can use <code>ls -1 | sort -n -k 1.6</code> to get the ones without 'k' or 'M' correct...</p>
  
  <ul>
  <li>file-k100.xml</li>
  <li>file-k10.xml</li>
  <li>file-k20.xml</li>
  <li>file-M100.xml</li>
  <li>file-M10.xml</li>
  <li>file-M20.xml</li>
  <li>file-10.xml</li>
  <li>file-20.xml</li>
  <li>file-100.xml</li>
  </ul>
  
  <p>I can use <code>ls -1 | sort -n -k 1.7</code> to get none of it correct</p>
  
  <ul>
  <li>file-100.xml</li>
  <li>file-10.xml</li>
  <li>file-20.xml</li>
  <li>file-k10.xml</li>
  <li>file-M10.xml</li>
  <li>file-k20.xml</li>
  <li>file-M20.xml</li>
  <li>file-k100.xml</li>
  <li>file-M100.xml</li>
  </ul>
  
  <p>Okay, fine. Let's really get it right. <code>ls -1 | grep "file-[0-9]*\.xml" | sort -n -k1.6 &amp;&amp; ls -1 file-k*.xml | sort -n -k1.7 &amp;&amp; ls -1 file-M*.xml | sort -n -k1.7</code></p>
  
  <ul>
  <li>file-10.xml</li>
  <li>file-20.xml</li>
  <li>file-100.xml</li>
  <li>file-k10.xml</li>
  <li>file-k20.xml</li>
  <li>file-k100.xml</li>
  <li>file-M10.xml</li>
  <li>file-M20.xml</li>
  <li>file-M100.xml</li>
  </ul>
  
  <p>Whew! Boy glad the "power of the linux command line" saved me there. (This isn't practical for my situation, because instead of <code>ls -1</code> I have a command that is another line or two long)</p>
  
  <p>Now, the Windows behavior is simple, elegant, and does what you want it to do 99% of the time. Why can't I have that in linux? Why oh why does <code>sort</code> not have a "automagic sort numbers in a way that doesn't make me bang head into wall" switch?</p>
  
  <p>Here's the pseudo-code for C++:</p>
  
  <pre><code>bool compare_two_strings_to_avoid_head_injury(string a, string b)
  {
      string::iterator ai = a.begin();
      string::iterator bi = b.begin();
      for(; ai != a.end() &amp;&amp; bi != b.end(); ai++, bi++)
      {
          if (*ai is numerical)
              gobble up the number incrementing ai past numerical chars;
          if (*bi is numerical)
              gobble up the number incrementing bi past numerical chars;
          actually compare *ai and *bi and/or the gobbled up number(s) here
              to determine if we need to compare more chars or can return the 
              answer now;
      }
      return something here;
  }
  </code></pre>
  
  <p>Was that so hard? Can someone put this in sort and send me a copy? Please?</p>