<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Many thanks.
<br>sam
<br>chris@onca.catsden.net wrote:
<blockquote TYPE=CITE>On Tue, 6 Feb 2001, Sam Wun wrote:
<p>> Hi,
<br>>
<br>> Does anyone know what is the quickly way to sort a list of IP addresses?
<br>>
<br>> ie. 203.21.254.89 should be larger than 203.21.254.9
<p>This may not be the fastest way, but it works, and its elegant :)
<p>import string
<p>def cmp_ipaddress ( ip1, ip2 ):
<br> parts1 = map(lambda x:int(x),
string.split(ip1,'.'))
<br> parts2 = map(lambda x:int(x),
string.split(ip2,'.'))
<br> comparisons = map ( lambda
x,y: cmp(x,y), parts1, parts2 )
<br> return reduce ( lambda x,y:
x or y, comparisons )
<p>ips = [ '203.21.254.89', '203.21.254.9' ]
<p>ips.sort ( cmp_ipaddress )
<p>print `ips`
<p>-> ['203.21.254.9', '203.21.254.89']
<p> ("`-/")_.-'"``-._
Ch'marr, a.k.a.
<br> . . `; -._ )-;-,_`) Chris
Cogdon <chmarr@furry.org.au>
<br> (v_,)' _ )`-.\ ``-'
<br> _.- _..-_/ / ((.' FC1.3:
FFH3cmA+>++C++D++H++M++P++R++T+++WZ++Sm++
<br>((,.-' ((,/ fL
RLCT acl+++d++e+f+++h++i++++jp-sm++</blockquote>
<pre>--
Sam Wun Firewalls / Security
Senior Software Engineer Electronic Commerce
eSec Limited Phone: +61 3 8371 5376
<A HREF="mailto:swun@eSec.com.au">mailto:swun@eSec.com.au</A> C++/JAVA/UNIX/OOP/OOD</pre>
</html>