views:

188

answers:

6

So we got this function in PHP

strcmp(string $1,string $2) // returns -1,0, or 1;

We Do not however, have an intcmp(); So i created one:

function intcmp($a,$b) {
    if((int)$a == (int)$b)return 0;
    if((int)$a  > (int)$b)return 1;
    if((int)$a  < (int)$b)return -1;
}

This just feels dirty. What do you all think?

this is part of a class to sort Javascripts by an ordering value passed in.

class JS
{
    // array('order'=>0,'path'=>'/js/somefile.js','attr'=>array());
    public $javascripts = array(); 
    ...
    public function __toString()
    {
        uasort($this->javascripts,array($this,'sortScripts'));
        return $this->render();
    }
    private function sortScripts($a,$b)
    {
        if((int)$a['order'] == (int)$b['order']) return 0;
        if((int)$a['order'] > (int)$b['order']) return 1;
        if((int)$a['order'] < (int)$b['order']) return -1;
    }
    ....
}
A: 

I wouldn't call it dirty per se, it seems valid enough. But I can't think where I would use that function. My only suggestion might be to include else:

function intcmp($a,$b)
{
    if((int)$a == (int)$b)return 0;
    else if((int)$a  > (int)$b)return 1;
    else if((int)$a  < (int)$b)return -1;
}
JYelton
+1  A: 

At a glance, yes it feels dirty. Except there must be a good reason you wrote that instead of just using the actual ==, >, and < operators. What was the motivation for creating this function?

If it were me, I'd probably just do something like:

$x = $a==$b ? 0 : ($a>$b ? 1 : ($a<$b ? -1 : null));

I realize this is just as ugly, and the : null; - not sure if PHP requires it or if I could have just done :; but I don't like it and that code should never execute anyway... I think I'd be a lot less confused about this if I knew the original requirements!

FrustratedWithFormsDesigner
+1, there likely was no motivation here, `intcmp` doesn't make much sense in PHP.
Andy E
@Andy E's head: I'm *guessing* the motivation was a function that produced similar results to `strcmp`, but I'd have to understand the scenario that led to that idea.
FrustratedWithFormsDesigner
The idea was to aid in sorting an array of a javascripts multi-dimensional array.
Chase
A: 

This code doesn't help you actually compare integers because it returns an integer - you still have to use a comparison operator to determine if the result is positive/negative/zero - so you might as well just compare the original values (which will also be considerably faster)

Jason Williams
+4  A: 

You could use

function intcmp($a,$b)
    {
    return ($a-$b) ? ($a-$b)/abs($a-$b) : 0;
    }

Although I don't see the point in using this function at all

nico
+1, Clever! :-)
FrustratedWithFormsDesigner
+1 for miniaturization!
JYelton
This worked like a charm! Do you think it works within the context of the class? Or would there be a better implementation? I haven't found a good standard for sorting multi-dimensional arrays yet!!
Chase
It doesn't work when $a == $b, you divide by zero....
Nicolas Viennot
@Pay: Ooopsie! You're right! Corrected!
nico
It's clever oneliner and all, but 1 line is not always more simple than 3. I would be scratching my head if I came across this code just to realize 5 minutes later than all it does is returns +1/0/-1 with all those divisions and modulus. I would just go with original version tbh.
serg
+1  A: 

Does it have to be +1 and -1? If not, just return (int) $a - (int) $b. I don't like the divide that someone else recommended, and there's no need to check for all three cases. If it's not greater and not equal, it must be less than.

return (int) $a > (int) $b ? 1 : (int) $a == (int) $b ? 0 : -1;
tomlogic
+1  A: 

Sort your data with:

function sortScripts($a, $b)
{
    return $a['order'] - $b['order'];
}

Use $b-$a if you want the reversed order.

Nicolas Viennot
I had to use ($b-$a)*-1 for reverse order. but I digress. Thanks!
Chase