hello!
my [URL=”http://tribesquery.toocrooked.com”]site
i have a working sorting algorythm thanks to this board, but i need to take it a step further. how can i do a “secondary sort” in my list?
for instance, when i sort my servers by IP address, this works great. however, there are many servers that have the same IP address. my seconday sort would be the port number in numerical order. where would i make the modification? what premise would i use?
for instance, i’d want to turn:
[code]
192.168.1.2:00015
192.168.1.1:00002
192.168.1.1:00001
192.168.1.1:00012
192.168.1.1:00005
192.168.1.3:00001
192.168.1.2:00002
192.168.1.2:00001
into:
[code]
192.168.1.1:00001
192.168.1.1:00002
192.168.1.1:00005
192.168.1.1:00012
192.168.1.2:00001
192.168.1.2:00002
192.168.1.2:00015
192.168.1.3:00001
wherein my IPs remain in order, but in similar IPs, the order is determined by the port.
Thanks for any insight!
[code=php]// compare function
function cmpi($a, $b)
{
global $sort_field;
$case = (isset($a[$sort_field]) ? 2 : 0) + (isset($b[$sort_field]) ? 1 : 0);
switch($case)
{
case 2:
return -1;
case 1:
return 1;
default:
continue;
}
if($sort_field == “ServerIP”)
return sprintf(‘%u’, ip2long($a[$sort_field])) < sprintf(‘%u’, ip2long($b[$sort_field]));
else if($sort_field == “Version”)
return sprintf(‘%f’, @$a[$sort_field]) > sprintf(‘%f’, @$b[$sort_field]);
else if(gettype(@$a[$sort_field]) == “double” && gettype(@$b[$sort_field]) == “double”)
return $a[$sort_field] > $b[$sort_field];
else if(gettype(@$a[$sort_field]) == “integer” && gettype(@$b[$sort_field]) == “integer”)
return $a[$sort_field] < $b[$sort_field];
else
return strcasecmp(@$a[$sort_field], @$b[$sort_field]);
}
usort($TEMP_SESSION, ‘cmpi’);