def fields_sort_key_factory(fields, field_order):
    """Return a function to sort `fields` by the `field_order`."""
    raw_field_order = [x[0] for x in fields]

    def key(key):
        name, field = key
        try:
            return field_order.index(name)
        except ValueError:
            return 100000 + raw_field_order.index(name)
    return key