programing tip

PHP에서 정수 (intcmp)에 해당하는 strcmp

itbloger 2020. 11. 27. 07:52
반응형

PHP에서 정수 (intcmp)에 해당하는 strcmp


그래서 우리는이 함수를 PHP로 얻었습니다.

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

그러나 우리는 intcmp (); 그래서 나는 하나를 만들었습니다.

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;
}

이건 그냥 더러워 요. 다들 어떻게 생각하세요?

전달 된 순서 값에 따라 자바 스크립트를 정렬하는 클래스의 일부입니다.

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;
    }
    ....
}

데이터 정렬 :

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

반대로 주문하려면 $ b- $ a를 사용하십시오.

문제의 숫자가 PHP의 정수 범위를 초과하면 return ($a < $b) ? -1 : (($a > $b) ? 1 : 0)더 강력합니다.


당신은 사용할 수 있습니다

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

이 기능을 사용하는 요점은 전혀 모르겠지만


순전히 일부 추가 정보로서 이에 대한 승인 된 RFC가 있습니다 ( https://wiki.php.net/rfc/combined-comparison-operator ).

따라서 비교 기능은 ...

<?php
$data = [...];
usort($data, function($left, $right){ return $left <=> $right; });
?>

A few really nice feature here is that the comparison is done in exactly the same way as all other comparisons. So type juggling will happen as expected.

As yet, there is no magic __forCompare() like method to allow an object to expose a comparison value. The current proposal (a different RFC) is to have each object be injected into every other object during the comparison so that it does the comparison - something which just seems odd to me - potential opportunity for recursion and stack overflow ... ! I would have thought either injecting the type of object for comparison (allowing an object the ability to represent appropriate values depending upon the type of comparison) or a blind request for a value that the object can serve up for comparison, would have been a safer solution.

Not yet integrated into PHP-NG (PHP 7 at the moment), but hopefully will be soon.


why reinventing the wheel? http://php.net/manual/en/function.strnatcmp.php

echo strnatcmp(1, 2) . PHP_EOL; // -1
echo strnatcmp(10, 2) . PHP_EOL; // 1
echo strnatcmp(10.5, 2) . PHP_EOL; // 1 - work with float numbers
echo strnatcmp(1, -2) . PHP_EOL; // 1 - work with negative numbers

Test it here: https://3v4l.org/pSANR


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;

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;
}

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!

참고URL : https://stackoverflow.com/questions/2852621/strcmp-equivelant-for-integers-intcmp-in-php

반응형