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 x+0)<<16) + (($z x+0)<<8) + $t] = $A;
instead, though. If you wanted 'em in REVERSE order, use krsort()
instead of ksort().
Usual 'untested code' warnings apply.
- steve
At 2:15 PM +1000 8/23/01, Chris Fry <[EMAIL PROTECTED]> wrote:
>Chris,
>
>Understand the problem now.
>
>All you need to keep is the last octect of the IP Address.
>
>The following code snippet generates an array entry for each ip. The array is
>then sorted and the result prepended with the base IP.
>
><?
>$strBaseIP = "127.0.0.";
> for ($i=0;$i<=255;$i++) {
> $strIP = $i;
> print "IP = $strIP<br>";
> $aryIP[$i] = $strIP;
>}
>sort($aryIP);
>for ($i=0;$i<=255;$i++) {
> $strIP = $strBaseIP.$aryIP[$i];
> print "IP = $strIP<br>";
>}
>?>
>
>Pretty rough and ready but it should point you in the right direction.
>
>If you need to sort IP's from differnet subnets then you need to
>split the IP as
>per the previous suggestion.
>
>Chris
>
>Chris Aitken wrote:
>
>> Hi all,
>>
>> Just a quick one,
>>
>> I am pulling a whole Class C list of IP addresses, and im trying to sort
>> them in order, but im getting it displayed
>>
>> 127.0.0.1
>> 127.0.0.10
>> 127.0.0.100
>> 127.0.0.101
>> 127.0.0.102
>>
>> etc. Is there any other way to sort this thinking they are numbers, rather
>> than text ?
>>
>> Thanks
>>
>> Chris
>>
>> --
>> Chris Aitken - Administration/Database Designer - IDEAL Internet
>> email: [EMAIL PROTECTED] phone: +61 2 4628 8888 fax: +61 2 4628 8890
>> __-----------------------------------------__
>> *** Big Brother ***
>> It just shows that the dull will rule the world. And we will be watching it.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>--
>
>Chris Fry
>Quillsoft Pty Ltd
>Specialists in Secure Internet Services and E-Commerce Solutions
>10 Gray Street
>Kogarah
>NSW 2217
>Australia
>
>Phone: +61 2 9553 1691
>Fax: +61 2 9553 1692
>Mobile: 0419 414 323
>eMail: [EMAIL PROTECTED]
>http://www.quillsoft.com.au
>
>You can download our Public CA Certificate from:-
>https://ca.secureanywhere.com/htdocs/cacert.crt
>
--
+------------------------ Open source questions? ------------------------+
| Steve Edberg University of California, Davis |
| [EMAIL PROTECTED] Computer Consultant |
| http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |
+----------- http://pgfsun.ucdavis.edu/open-source-tools.html -----------+
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]