-2

I am trying to sort the list of ipaddress from the following list.

IPlist= ['209.85.238.4', '216.239.51.98', '64.233.173.198', '64.3.17.208', '64.233.173.238']

#1st case

tmp1 = [list (map (str, ip.split("."))) for ip in IPlist]

tmp1.sort()
print(tmp1)

When I run this snippet. I got the following output.

[['209', '85', '238', '4'], ['216', '239', '51', '98'], ['64', '233', '173', '198'], ['64', '233', '173', '238'], ['64', '3', '17', '208']]

#second case

tmp = [tuple (map (int, ip.split("."))) for ip in IPlist]
# print(tmp)

tmp.sort ()
print(tmp)

When I run the second case,I got the following output.

[(64, 3, 17, 208), (64, 233, 173, 198), (64, 233, 173, 238), (209, 85, 238, 4), (216, 239, 51, 98)]

The only thing that I found the difference is the conversion to either string or int function using above function. But even if the intial values are string, doesnt sort() function works in the same way?

For eg:

lst = ['23', '33', '11', '7', '55']
 
# Using sort() function with key as int
lst.sort(key = int)
 
print(lst)
Output: 

['7', '11', '23', '33', '55']
5
  • 4
    Clearly not. str(11) < str(7) Commented Jan 22, 2023 at 19:54
  • I dont understand, the answer says ['7', '11', '23', '33', '55'], so clearly '7' < '11' Commented Jan 22, 2023 at 20:30
  • 1
    No; int('7') < int('11'). That's what key=int does. Commented Jan 22, 2023 at 20:32
  • inet_aton to sort IPv4 addresses. sorted(IPlist, key=inet_aton) Commented Jan 22, 2023 at 20:46
  • you could use sort with a key formed of a tuple of ints given that your addresses contain multiple integers separted by periods: sorted(IPlist,key=lambda a:tuple(map(int,a.split('.')))). The tuple comparisons will use numeric values but the output will remain a list of the original strings Commented Jan 23, 2023 at 5:19

1 Answer 1

7

This happens because the addresses are being compared as strings, which are sorted lexically - converting them to numbers will sort the way you expect

>>> sorted(["11", "2"])
['11', '2']
>>> sorted([11, 2])
[2, 11]

However, it's probably better overall to use the builtin ipaddress module, which can sort the addresses correctly for you and has useful methods and comparisons for IP addresses (such as checking whether an IP is a member of a subnet or providing the network address of an interface with an IP and mask)

>>> import ipaddress
>>> ip_list = ['209.85.238.4', '216.239.51.98', '64.233.173.198', '64.3.17.208', '64.233.173.238']
>>> sorted(ipaddress.ip_address(a) for a in ip_list)
[IPv4Address('64.3.17.208'), IPv4Address('64.233.173.198'), IPv4Address('64.233.173.238'), IPv4Address('209.85.238.4'), IPv4Address('216.239.51.98')]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.