[BangPypers] How to sort the IP(s)

Anand Balachandran Pillai abpillai at gmail.com
Thu May 8 11:17:24 CEST 2008


Do you really need any kind of additional processing ?

The basic sort algorithm is smart enough to do this by itself,
>>> l=['192.168.1.1','172.18.13.2','192.168.3.2','172.19.2.1']
>>>l.sort()
>>>l
['172.18.13.2', '172.19.2.1', '192.168.1.1', '192.168.3.2']

or use sorted(...) if you don't want to modify in place...

Here is an even closer example to demo this...
>>> l=['192.168.12.21','192.168.12.15','192.168.11.10','192.168.10.5','192.168.15.1','192.167.10.1']
>>> sorted(l)
['192.167.10.1', '192.168.10.5', '192.168.11.10', '192.168.12.15',
'192.168.12.21', '192.168.15.1']


--Anand

On Thu, May 8, 2008 at 2:41 PM, Anand Chitipothu <anandology at gmail.com> wrote:
> On Thu, May 8, 2008 at 11:53 PM, Kushal Das <kushaldas at gmail.com> wrote:
>
> > Hi,
>  >  What is the best way to sort IP numbers
>  >  numbers like
>  >  192.168.20.1
>  >  192.168.1.1
>  >  172.18.13.2
>
>  ips = ['192.168.20.1', '192.168.1.1', '172.18.13.2']
>  sorted(ips, key=lambda ip: [int(x) for x in ip.split('.')])
>
>  # ips.sort(key=lambda ip: [int(x) for x in ip.split('.')]) if you want
>  to sort in-place.
>
>
> _______________________________________________
>  BangPypers mailing list
>  BangPypers at python.org
>  http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
-Anand


More information about the BangPypers mailing list