Hello -
Looking to find a function that works just like the php function uasort() but have it maintain the relative order of items when equal keys are found.
Here's my code now:
$array = array("item1"=>-1,"item2"=>-1,"item3"=>-1,"item4"=>0,"item5"=>2,"item6"=>2,"item7"=>1);
function sortThis($a,$b)
{
if( $a == -1 || $b == -1 )
{
return 0;
}
elseif ($a == $b)
{
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($array,"sortThis");
As you can see from the code above, I'm also returning 0 if negative #'s are found since all negative numbers must stay relative to where they are in the list (just like equal #). However, since php removed stable sort and do not keep the original order for elements comparing as equal, the code above returns:
array(7) {
["item1"]=>
int(-1)
["item3"]=>
int(-1)
["item4"]=>
int(0)
["item7"]=>
int(1)
["item6"]=>
int(2)
["item5"]=>
int(2)
["item2"]=>
int(-1)
}
Item2 was moved down the list when it did not need to be moved since it's compared as an equal #. What I need returned is:
array(7) {
["item1"]=>
int(-1)
["item2"]=>
int(-1)
["item3"]=>
int(-1)
["item4"]=>
int(0)
["item7"]=>
int(1)
["item6"]=>
int(2)
["item5"]=>
int(2)
}
Any help appreciated!
Thanks,
Andrew Schools