I'm trying to automatically compare BAM files being output by bowtie2 (for a continuous integration system). Currently using `bamUtil diff` which works well, except I'm noticing sometimes 2 lines in the BAM file will have the same name/start position, but represent different reads. For whatever reason, bowtie2 sometimes reverses the order of the lines, so bamUtil's algorithm calls a mismatch. I see several potential solutions to this problem:

1. Get samtools to sort by another dimension in addition to position/name. Pretty much anything should do. Can this be done? I don't see any options in the man page.

2. Use another comparision tool than bamUtil which doesn't have this problem. Honestly a stand-alone program for diffing would be best for me anyway and I'm considering writing one.

3. Find some way to get bowtie2 to be more deterministic in its ordering. Any ideas here? 
I kinda feel like an idiot right now. Upon further reflection on my task, I realized that in this particular case there's no reason to limit myself to samtools' method of sorting. I can simply convert my two BAM files to SAM (without the headers), then feed them to GNU coreutils sort, then diff them normally. Seems to be working. Here's a bash script that does it:

    #!/bin/bash
    # bamdiff.sh    

    f1=${1}
    f2=${2}
    
    f1TempHeaderlessSAM=.bamdiff_f1TempHeaderlessSAM
    f2TempHeaderlessSAM=.bamdiff_f2TempHeaderlessSAM
    
    samtools view ${f1} | sort > ${f1TempHeaderlessSAM}
    samtools view ${f2} | sort > ${f2TempHeaderlessSAM}
    
    diff ${f1TempHeaderlessSAM} ${f2TempHeaderlessSAM}
    
    diffExitCode=$?
    
    rm ${f1TempHeaderlessSAM}
    rm ${f2TempHeaderlessSAM}
    
    exit ${diffExitCode}

