Created
February 13, 2023 14:33
-
-
Save bockor/fd1871f97adc9eae3064c96811b19951 to your computer and use it in GitHub Desktop.
python sorting ipaddresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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