Re: Sorting IP Address Data (oops!)
| From: | Steve Edberg | Date: | Thu, 23 Aug 2001 07:39:25 +0000 |
| Subject: | Re: Sorting IP Address Data (oops!) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-64053@lists.php.net to get a copy of this message | ||
And if the numbers are from different subnets, you could sort like so (assuming addresses are in a 1-dimensional array, here called $IpAddresses):
function sort_address($IpAddresses) {
$List = array();
foreach ($IpAddresses as $A) {
list($x,$y,$z,$t) = explode('.', $A);
$List[($x*16777216) + ($y*6553616) + ($z*256) + $t] = $A;
}
ksort($List, SORT_NUMERIC);
return array_values($List);
}
My first thought was to use the line
$List[($x<<24) + ($y<<16) + ($z<<8) + $t] = $A;instead, and gain a little speed advantage by bitshifting instead of multiplying, but then you'd need to force $x, $y, $z and $t to integers first. You might be able to do
$List[(((int)$x)<<24) + (((int)$y)<<16) + (((int)$z)<<8) + $t] = $A;or
$List[(($x+0)<<24) + (($y+0)<<16) + (($z+0)<<8) + $t] = $A;**************************************************************************** I MADE A BIT OF A CUT'N'PASTE ERROR IN THE LINE ABOVE IN MY PREVIOUS EMAIL! **************************************************************************** instead, though. If you wanted 'em in REVERSE order, use krsort() instead of ksort(). Usual 'untested code' warnings apply. - steve -- +------------------------ Open source questions? ------------------------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +----------- http://pgfsun.ucdavis.edu/open-source-tools.html -----------+