
japhy
Enthusiast
/ Moderator
Apr 6, 2001, 7:46 AM
Post #3 of 4
(1033 views)
|
The fastest way to sort IP addresses is radix sort (look it up in your neighborhood friendly algorithm book). Here's a Perl implementation:
use Socket qw( inet_aton inet_ntoa ); sub IP_radix_sort { for (my $i = 3; $i >= 0; $i--) { my @table; for (@_) { push @{ $table[unpack "\@$i C", $_] }, $_; } @_ = map @$_, @table; } return @_; } sub IPsort { map inet_ntoa($_), IP_radix_sort map inet_aton($_), @_; } You call the function @sorted = IPsort @unsorted. Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author
(This post was edited by japhy on Apr 23, 2001, 7:16 AM)
|