<p>I should start by saying I have solved this problem, but I feel like my code is ugly and overkill.</p>

<p>I am trying to sort a dataframe on a column containing human chromosomes:</p>

<p>chr1,chr2...chrY, chrX.</p>

<p>the problem is:</p>

<p>chr1, is followed by chr10.  </p>

<p>What tricks do you use to deal with this problem <strong>IN the [R] environment?</strong></p>

<p>the mixedsort is pretty good, but it will think ChrM comes before ChrX, then ChrY</p>
<p>if you have your own arbitrary order you should just use factors</p>
<pre><code>&gt; df&lt;-data.frame("chr"=c("chr1","chrM","chr10","chr2","chrX","chr2"),"val"=c(1,2,3,4,5,6))
&gt; df
    chr val
1  chr1   1
2  chrM   2
3 chr10   3
4  chr2   4
5  chrX   5
6  chr2   6
&gt; chrOrder&lt;-c(paste("chr",1:22,sep=""),"chrX","chrY","chrM")
&gt; df$chr&lt;-factor(df$chr, levels=chrOrder)
&gt; df$chr
[1] chr1  chrM  chr10 chr2  chrX  chr2 
Levels: chr1 chr2 chr3 chr4 chr5 chr6 chr7 chr8 chr9 chr10 chr11 chr12 chr13 chr14 chr15 chr16 chr17 chr18 chr19 chr20 chr21 chr22 chrX chrY chrM
&gt; df[order(df$chr),]
    chr val
1  chr1   1
4  chr2   4
6  chr2   6
3 chr10   3
5  chrX   5
2  chrM   2
</code></pre>