def _get_contig_based_lessthan(contigs):
  """Returns a callable that compares variants on genomic position.

  The returned function takes two arguments, both of which should be Variant
  protos or None. The function returns True if and only if the first Variant is
  strictly less than the second, which occurs if the first variant is on a
  previous chromosome or is on the same chromosome and its entire span lies
  before the start position of the second variant. `None` is treated as a
  sentinel value that does not compare less than any valid Variant.

  Args:
    contigs: list(ContigInfo). The list of contigs in the desired sort order.

  Returns:
    A callable that takes two Variant protos as input and returns True iff the
    first is strictly less than the second. Note that if the variant has a
    contig not represented in the list of contigs this will raise IndexError.
  """
  contig_index = {contig.name: i for i, contig in enumerate(contigs)}

  def lessthanfn(variant1, variant2):
    if variant1 is None:
      return False
    if variant2 is None:
      return True
    contig1 = contig_index[variant1.reference_name]
    contig2 = contig_index[variant2.reference_name]
    return (contig1 < contig2 or
            (contig1 == contig2 and variant1.end <= variant2.start))

  return lessthanfn