title: Sorting by a slice of the text string
body: <p>I am wanting to sort my data but the standard Excel "A to Z" sort function isn't cutting it. I was hoping someone knew how to make a custom sort that could suit my needs. Here is a sample:</p>
  
  <pre><code>chrPos        count
  chr1_10000598   10
  chr1_10000647   10
  chr1_10001370   30
  chr1_10001390   30
  chr1_10001392   30
  chr1_10001414   30
  chr1_10001418   30
  chr1_10001473   10
  chr1_10001505   10
  chr1_10001516   20
  chr1_1000156    30
  </code></pre>
  
  <p>As you can see the last row is out of place when using the built in sort function, this should be the first row not the last one here. I think adding a second layer of sorting would to the trick but that layer would have to sort by ascending value based on the number that is following the underscore.  </p>
  
  <p>Any ideas? Would this possibly be easier with R instead?  </p>
  
  <p><em>Edit to add details from comments:</em>  </p>
  
  <p>Sorting is to be ascending on the numeric part after the underscore, within ascending on the chr numeric part (running from 1 to 22 both inclusive) and then chrM_, chrX_ and chrY_ in that order (also with their numeric parts sorted ascending).  </p>
  
  <p>The numeric part after the underscore may be up to 8 digits.</p>