def _sort_hostnames(hostname_list):
    '''
    sort minion ids favoring in order of:
        - FQDN
        - public ipaddress
        - localhost alias
        - private ipaddress
    '''
    # punish matches in order of preference
    punish = [
        'localhost.localdomain',
        'localhost.my.domain',
        'localhost4.localdomain4',
        'localhost',
        'ip6-localhost',
        'ip6-loopback',
        'ipv6-localhost',
        'ipv6-loopback',
        '127.0.2.1',
        '127.0.1.1',
        '127.0.0.1',
        '0.0.0.0',
        '::1',
        'fe00::',
        'fe02::',
    ]

    def _key_hostname(e):
        # should never have a space in hostname
        # favor hostnames w/o spaces
        if e in ' ':
            first = 1
        else:
            first = -1

        # punish localhost list
        if e in punish:
            second = punish.index(e)
        else:
            second = -1

        # punish ipv6
        third = e.count(':')

        # punish ipv4
        # punish ipv4 addresses that start with '127.' more
        e_is_ipv4 = e.count('.') == 3 and not any(c.isalpha() for c in e)
        if e_is_ipv4:
            if e.startswith('127.'):
                fourth = 1
            else:
                fourth = 0
        else:
            fourth = -1

        # favor hosts with more dots
        fifth = -(e.count('.'))

        # favor longest fqdn
        sixth = -(len(e))

        return (first, second, third, fourth, fifth, sixth)

    return sorted(hostname_list, key=_key_hostname)
dataset/ETHPy150Open saltstack/salt/salt/utils/network.py/_sort_hostnames/swapped operands of `in`