def keywords_first(keywords):
    """ Returns a sort key which preferrs values matching the given keywords
    before other values which are sorted alphabetically.

    """
    assert hasattr(keywords, 'index')

    def sort_key(v):
        try:
            return keywords.index(v) - len(keywords), ''
        except ValueError:
            return 0, v
    return sort_key