0

I have a table that looks like this:

list_ip=[
  ['192.168.1.15', 10, 20],
  ['192.168.0.20', 15, 15],
  ['10.0.0.5', 50, 5],
  ['10.0.0.2', 55, 5]]

The result should be:

[['10.0.0.2', 55, 5],
 ['10.0.0.5', 50, 5],
 ['192.168.0.20',15,15],
 ['192.168.1.15', 10, 20]]

When I have just list of IP addresses I can sort them using:

import ipaddress 
list_ip=sorted(list_ip, key = ipaddress.IPv4Address)

However I cannot figure out how to do it with extra columns.

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
gorte
  • 23
  • 1
  • 6

1 Answers1

2
import ipaddress 
list_ip = sorted(list_ip, key = lambda x: ipaddress.IPv4Address(x[0]))
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Perfect, thanks! I did not know about the `key = lambda x: ipaddress.IPv4Address(x[0])` thing – gorte Oct 22 '20 at 14:25