A little background. In the actual array I'm working with, the a, b, c's are primary keys. One shouldn't expect them to actually be in order. This example uses a, b, c to verify if a sort worked properly.
I want to sort the array so the result will have a,b,c,d,e,f,g,h, etc in order.
Notice that if you examine the $test array, looking from top to bottom, all generation 2 rows are in order. Same with 3, 4 and 5. The only problem with the current order is each generation is seperated by other "in order" groups of generations. If rows with a specific generation never switch positions, the result should be obtained. The fact that the input is a,b,c,d and the result is a, e d, b means that rows with like generation ARE in fact switching position. How can I prevent this? This isn't the way it's supposed to work, is it? My ultimate question is...why is this not working as I'd expect and do you have a suggestion on how to maintain the original order of each generation?
Here is my demo code:
<?php
function generationsort($a, $b) {
$agen=$a["Generation"];
$bgen=$b["Generation"];
return $agen - $bgen;
}
$test=array();
$test[a]["Generation"]=1;
$test[b]["Generation"]=2;
$test[c]["Generation"]=2;
$test[d]["Generation"]=2;
$test[e]["Generation"]=2;
$test[f]["Generation"]=3;
$test[g]["Generation"]=3;
$test[l]["Generation"]=4;
$test[m]["Generation"]=4;
$test[h]["Generation"]=3;
$test[i]["Generation"]=3;
$test[j]["Generation"]=3;
$test[k]["Generation"]=3;
$test[n]["Generation"]=4;
$test[s]["Generation"]=5;
$test[o]["Generation"]=4;
$test[p]["Generation"]=4;
$test[q]["Generation"]=4;
$test[t]["Generation"]=5;
$test[u]["Generation"]=5;
$test[v]["Generation"]=5;
$test[r]["Generation"]=4;
uasort ($test, "generationsort");
foreach ($test as $key => $value) {
echo "$key: " . $value["Generation"] . "\n<br>";
}
?>
and here are the results I get:
a: 1
e: 2
d: 2
b: 2
c: 2
j: 3
k: 3
h: 3
i: 3
f: 3
g: 3
p: 4
q: 4
r: 4
m: 4
o: 4
l: 4
n: 4
v: 5
t: 5
s: 5
u: 5
I'm running PHP version 4.3.10-16
Thanks for any help,
Kevin Nowaczyk