[Solved] How to sort/order tuple of ip address in python


You can do it like this:

a = [{'host': u'10.219.1.1'}, {'host': u'10.91.1.1'}, {'host': u'10.219.4.1'}, {'host': : '10.91.4.1'}]

sorted(a, key=lambda x: tuple(int(i) for i in x['host'].split('.')))

# [{'host': '10.91.1.1'}, {'host': '10.91.4.1'}, {'host': '10.219.1.1'}, {'host': '10.219.4.1'}]

sorted(a, key=lambda x: tuple(int(i) for i in x['host'].split('.'))[::-1])

# [{'host': '10.91.1.1'}, {'host': '10.219.1.1'}, {'host': '10.91.4.1'}, {'host': '10.219.4.1'}]

2

solved How to sort/order tuple of ip address in python