Skip to content

Instantly share code, notes, and snippets.

@bockor
Created February 13, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bockor/fd1871f97adc9eae3064c96811b19951 to your computer and use it in GitHub Desktop.
Save bockor/fd1871f97adc9eae3064c96811b19951 to your computer and use it in GitHub Desktop.
python sorting ipaddresses
# try to sort ipv4 addr as strings
ss = ['114.122.102.2','114.122.11.1','118.123.12.13','122.14.113.3','192.144.1.5']
print([str(s) for s in sorted(ss)])
# this does not look righ to me ... let's invoke the ipaddress module
from ipaddress import IPv4Address
# and create the ipv4 addr instances
sso = [IPv4Address(s) for s in ss]
#print the sorted ipv4 addr
print([str(s) for s in sorted(sso)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment