Sort chromosome

There are two rules that you might want to sort your chromosome by:
– Sort in a case-sensitive lexicographical order (eg. chr1, chr10, chr11 …), usually required by bedtools, UCSC utilities (eg. bedGraphToBigWig etc.) and so on;
– Or sort in a numeric order for other purpose (eg. chr1, chr2, chr3 …).

Sort in lexicographical order:

sort -k1,1 -k2,2n input.bed > sorted.bed
sortBed input.bed > sorted.bed # bedtools function
LC_COLLATE=C sort -k1,1 -k2,2n input.bedgraph > sorted.bedgraph

Sort in numeric order:

./sort_chr.sh input.bed

Download the code from here:
https://github.com/bioinfocore/bashCore/blob/master/sort_chr.sh

Source:
https://www.biostars.org/p/150036/
http://seqanswers.com/forums/showthread.php?t=63932