'A comparison function for sorting Host objects by hostname.
This strips any trailing numeric digits, ignores leading 0s and
compares hostnames by the leading name and the trailing digits as a
number.  If both hostnames do not match this pattern, they are simply
compared as lower case strings.
Example of how hostnames will be sorted:
alice, host1, host2, host09, host010, host10, host11, yolkfolk
This hopefully satisfy most people\'s hostname sorting needs regardless
of their exact naming schemes.  Nobody sane should have both a host10
and host010 (but the algorithm works regardless).'

@classmethod
def cmp_for_sort(cls, a, b):

    lower_a = a.hostname.lower()
    lower_b = b.hostname.lower()
    match_a = cls._ALPHANUM_HOST_RE.match(lower_a)
    match_b = cls._ALPHANUM_HOST_RE.match(lower_b)
    if (match_a and match_b):
        (name_a, number_a_str) = match_a.groups()
        (name_b, number_b_str) = match_b.groups()
        number_a = int(number_a_str.lstrip('0'))
        number_b = int(number_b_str.lstrip('0'))
        result = cmp((name_a, number_a), (name_b, number_b))
        if ((result == 0) and (lower_a != lower_b)):
            return cmp(lower_a, lower_b)
        return result
    else:
        return cmp(lower_a, lower_b)
