def sort_bamfile(job, bamfile, sample_type, univ_options, samtools_options):
    work_dir = os.getcwd()
    in_bamfile = ''.join([sample_type, '.bam'])
    out_bamfile = '_'.join([sample_type, 'sorted.bam'])
    input_files = {
        in_bamfile: bamfile}
    input_files = get_files_from_filestore(job, input_files, work_dir, docker=True)
    parameters = ['sort',
                  '-o', docker_path(out_bamfile),
                  '-O', 'bam',
                  '-T', 'temp_sorted',
                  '-@', str(samtools_options['n']),
                  input_files[in_bamfile]]
    docker_call(tool='samtools', tool_parameters=parameters, work_dir=work_dir,
                dockerhub=univ_options['dockerhub'], tool_version=samtools_options['version'])
    job.fileStore.deleteGlobalFile(bamfile)
    job.fileStore.logToMaster('Ran samtools-sort on %s:%s successfully'
                              % (univ_options['patient'], sample_type))
    return job.fileStore.writeGlobalFile(out_bamfile)