/    Sign up×
Community /Pin to ProfileBookmark

[sorting] how can i do a secondary sort in my case?

hello!

my [URL=”http://tribesquery.toocrooked.com”]site[/URL] grabs a list of servers from a game. you can then elect to sort the list.

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[/code]

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[/code]

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’);[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderJun 29.2007 — [code=php]
<?php
define("NEWLINE", "n");

$input = "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";

$servers = array();
$ip_addresses = array();
$ports = array();
$lines = explode(NEWLINE, $input); // Explode each line

foreach($lines as $combined)
{
list($ip_address, $port) = explode(":", $combined);
$servers[] = array( "ip_address" => $ip_address,
"port" => $port);
}

foreach($servers as $key => $value)
{
$ip_addresses[$key] = $value["ip_address"];
$ports[$key] = $value["port"];
}

?>
<h3>Unsorted</h3>
<?php
draw_servers($servers);
?>


<h3>Sorted IP Addresses</h3>
<?php
array_multisort($ip_addresses, SORT_ASC, $ports, SORT_ASC, $servers);
draw_servers($servers);
?>


<h3>Sorted Ports</h3>
<?php
array_multisort($ports, SORT_ASC, $ip_addresses, SORT_ASC, $servers);
draw_servers($servers);

///////////////////////////////////////////////////////////////////////////////

function draw_servers($servers)
{
?>
<table>
<?php
foreach($servers as $server)
{
?>
<tr>
<td><?php echo $server["ip_address"]; ?></td>
<td><?php echo $server["port"]; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
[/code]


You may need to change the define for NEWLINE to your systems, linux "n" or windows "rn", I forget what MAC's Use..
Copy linkTweet thisAlerts:
@MrCoderJun 29.2007 — [CODE]
Unsorted
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
Sorted IP Addresses
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
Sorted Ports
192.168.1.1 00001
192.168.1.2 00001
192.168.1.3 00001
192.168.1.1 00002
192.168.1.2 00002
192.168.1.1 00005
192.168.1.1 00012
192.168.1.2 00015
[/CODE]


Tested, seems to work ok?
Copy linkTweet thisAlerts:
@Angry_Black_ManauthorJun 29.2007 — thanks for such a prompt response... i will let you know how it turns out!! thanks for testing too. i appreciate the time you took!
×

Success!

Help @Angry_Black_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...