def ip_key(key):
    """Function to make a 'canonical' IP string for sorting.
    The given IP has each subfield expanded to 3 numeric digits, eg:

        given '1.255.24.6' return '001.255.014.006'
    """

    ip = key[0]
    fields = ip.split('.')
    result = []
    for f in fields:
        result.append('%03d' % int(f))

    return result