Hi,

I have my bam file in this order:

    samtools view -H my.bam
    
    @CO     Duplicates marked (bamUtil Version: 1.0.2)
    @SQ     SN:chr1 LN:249250621    AS:hg19
    @SQ     SN:chr10        LN:135534747    AS:hg19
    @SQ     SN:chr11        LN:135006516    AS:hg19
    @SQ     SN:chr12        LN:133851895    AS:hg19
    @SQ     SN:chr13        LN:115169878    AS:hg19
    @SQ     SN:chr14        LN:107349540    AS:hg19
    @SQ     SN:chr15        LN:102531392    AS:hg19
    @SQ     SN:chr16        LN:90354753     AS:hg19
    @SQ     SN:chr17        LN:81195210     AS:hg19
    @SQ     SN:chr18        LN:78077248     AS:hg19
    @SQ     SN:chr19        LN:59128983     AS:hg19
    @SQ     SN:chr2 LN:243199373    AS:hg19
    @SQ     SN:chr20        LN:63025520     AS:hg19
    @SQ     SN:chr21        LN:48129895     AS:hg19
    @SQ     SN:chr22        LN:51304566     AS:hg19
    @SQ     SN:chr3 LN:198022430    AS:hg19
    @SQ     SN:chr4 LN:191154276    AS:hg19
    @SQ     SN:chr5 LN:180915260    AS:hg19
    @SQ     SN:chr6 LN:171115067    AS:hg19
    @SQ     SN:chr7 LN:159138663    AS:hg19
    @SQ     SN:chr8 LN:146364022    AS:hg19
    @SQ     SN:chr9 LN:141213431    AS:hg19
    @SQ     SN:chrM LN:16571        AS:hg19
    @SQ     SN:chrX LN:155270560    AS:hg19
    @SQ     SN:chrY LN:59373566     AS:hg19
    @PG     ID:bwa  PN:bwa  VN:0.6.1-r104

And I wanna order it as, this:

https://gist.github.com/RamRS/510a730a4b77f8ded1d95acb97067e3c

How can I sort it in this order and arrange it?

Thank you
use [picard **ReorderSam**](http://picard.sourceforge.net/command-line-overview.shtml)

> ReorderSam reorders reads in a SAM/BAM file to match the contig ordering in a provided reference file, as determined by exact name matching of contigs