tags:

views:

159

answers:

1

We wrote some code involving usort which works fine on our development systems (PHP 5.2.8), but are experiencing a problem on our live systems (PHP 5.2.0):

// Sort by distance
usort($locations, 'Interpolator::sortByDistance');

calls the method (within the same class Interpolator):

private static function sortByDistance($a, $b) {
    $return = 0;
    if($a['distance'] > $b['distance']) {
        $return = 1;
    } else if ($a['distance'] < $b['distance']) {
        $return = -1;
    }
    return $return;
}

On our live systems, this returns a completely arbitrarily sorted array, the original order is disturbed, but still not sorted by distance.

I cannot find any reference to a PHP bug fixed between 5.2.0 and 5.2.8 relevant to this problem.

Where might this problem be coming from? Can I fix this short of writing a sorting function myself?

+3  A: 

The only think I can think of is that you should be using this:

usort($locations, array('Interpolator', 'sortByDistance'));
Greg
Thanks, but like chaos commented on my question, that's a 5.2.3-feature.
christian studer
Umm, wait, that actually worked...
christian studer
Nice. Guess they broke the support somewhere along the road to 5.8.0.
chaos
Oops, didn't see the question comments. 5.8.0 doesn't exist - I guess you meant 5.2.0 on the server, so before 'Interpolator::sortByDistance' style was added.
Greg
@chaos: I just assumed 5.8.0 was a typo and he meant 5.2.0. 5.2.8 is the latest version of PHP.
R. Bemrose
Seems related to http://bugs.php.net/bug.php?id=29049 but claims to have been fixed 5 years ago!
Greg
Guess I should've gone with my first thought, "uhh are you *sure* you're running 5.8.0?"
chaos