开发者

uasort Issues - Maximum Value to output first

So if I had 2 columns in a object

A -> values are 10 8 6 4

B -> values are 9 7 5 3

I want to merge B into A with 9 below 10, 7 below 8 etc.

uasort($TopConsumers,array('Utilities','orderconsumers'));
public static function orderConsumers($TopConsumers)
{
    $a = $Top开发者_如何学PythonConsumers->outTotal;
    $b = $TopConsumers->inTotal;
    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? -1 : +1;
}

Why is this not working?


You need to merge the arrays first, then sort them.

$merged = array_merge($TopConsumers->outTotal, $TopConsumers->inTotal);
sort($merged);


Your sorting function needs to have $a and $b as arguments to it. Not the array itself.

public static function orderConsumers($a, $b)
{
    if ($a->outTotal == $b->inTotal) {
        return 0;
    }
    return ($a->outTotal > $b-inTotal) ? -1 : +1;
}

That said it seems a little odd to sort by two columns. I'm not sure this is going to give you what you're looking for. Normally you'd compare the same column name in a user sort function like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜