【发布时间】:2015-09-03 07:00:19
【问题描述】:
我正在尝试在 PHP 中对投票列表进行排序。
列表是一个数组,包含类对象:
Array
(
[0] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => bbh - bghs dsdw
)
[votes] => SimpleXMLElement Object
(
[0] => 6
)
[key] => SimpleXMLElement Object
(
[0] => bbh--0
)
)
[1] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => aaa - bbb
)
[votes] => SimpleXMLElement Object
(
[0] => 4
)
[key] => SimpleXMLElement Object
(
[0] => aaa--0
)
)
[2] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => wdewv - qwdqs
)
[votes] => SimpleXMLElement Object
(
[0] => 3
)
[key] => SimpleXMLElement Object
(
[0] => wdewv--0
)
)
[3] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => Hsg and fdSv - aGamaama
)
[votes] => SimpleXMLElement Object
(
[0] => 2
)
[key] => SimpleXMLElement Object
(
[0] => hsgandfdsv--0
)
)
)
我设法通过->key 在那里进行排序,工作正常:
usort($votedsongs, function ($a, $b) { return $b->votes - $a->votes; });
但是在这之后,我仍然需要另一个排序函数来对->title投票数相同的歌曲进行排序。
我已经找到了一些可以处理类似问题的解决方案,但这些对我不起作用。
对此有何想法?
【问题讨论】:
-
` if (!($r = $b->votes - $a->votes)) $r = next_rules;返回 $r;`
-
请解释一下,我不知道如何实现。
-
if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r; -
所以我替换了我的 usort 函数,例如:
usort($votedsongs, function ($a, $b) { if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r; });对吗?可悲的是,这并没有奏效.. -
把
title改成titel,结果是什么?