Apr-18-2019, 12:00 PM
You want to sort by: (port, right_ip_address, left_ip_address)
Here is a key function to sort them.
The sorter grabs port, addr1 and addr2. addr1/2 are converted to an ip_address object, then it's converted to an integer.
The port must be also an integer.
finally the sorter returns a tuple with 3 integers. The sorted function uses this information to get the items in the right order.
Here is a key function to sort them.
import ipaddress def ip_sorter(text): parts = text.split() port = int(parts[-1]) addr1 = int(ipaddress.ip_address(parts[-3])) addr2 = int(ipaddress.ip_address(parts[3])) return port, addr1, addr2Then call:
sorted(your_set, key=ip_sorter)For each element in
your_set
, the key function is called with the element as argument.The sorter grabs port, addr1 and addr2. addr1/2 are converted to an ip_address object, then it's converted to an integer.
The port must be also an integer.
finally the sorter returns a tuple with 3 integers. The sorted function uses this information to get the items in the right order.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!