code: def _sort_by_region(fnames, regions, ref_file, config):
      contig_order = {}
      for i, sq in enumerate(ref.file_contigs(ref_file, config)):
          contig_order[sq.name] = i
      sitems = []
      assert len(regions) == len(fnames), (regions, fnames)
      added_fnames = set([])
      for region, fname in zip(regions, fnames):
          if fname not in added_fnames:
              if isinstance(region, (list, tuple)):
                  c, s, e = region
              elif isinstance(region, six.string_types) and region.find(":") >= 0:
                  c, coords = region.split(":")
                  s, e = [int(x) for x in coords.split("-")]
              else:
                  c = region
                  s, e = 0, 0
              sitems.append(((contig_order[c], s, e), c, fname))
              added_fnames.add(fname)
      sitems.sort()
      return [(x[1], x[2]) for x in sitems]