sorting on IP addresses

Justin Sheehy justin at iago.org
Wed Mar 7 05:29:50 CET 2001


Sam Wun <swun at esec.com.au> writes:

> Does anyone know what is the quickly way to sort a list of IP addresses?
> 
> ie. 203.21.254.89 should be larger than 203.21.254.9

Convert to longs and sort on those values?

Not all that quick, I suppose, but I don't know of a faster correct way.

I suppose you could do it one octet at a time, which might be faster
for cases like your example where the addresses are fairly close.

The actual code for either of those approaches is short and
straightforward.  Just write a little function to compare two
addresses using the method of your choice, and pass it to your list's
sort method.

-Justin

 




More information about the Python-list mailing list