def _sort_by_length(key_value):
    """Sort key, value pairs to prioritize shortest substitution

    - prioritize long strings first to ensure the longest match is substituted
    - prioritize short variable names in case two keys have the same value
    """
    key, value = key_value
    return (-len(value), len(key))