【发布时间】:2012-01-25 18:00:12
【问题描述】:
我有以下数组:
Array
(
[0] => stdClass Object
(
[timestamp] => 1
[id] => 10
)
[1] => stdClass Object
(
[timestamp] => 123
[id] => 1
)
[2] => stdClass Object
(
[timestamp] => 123
[id] => 2
)
)
我目前正在使用以下代码按时间戳属性对数组进行排序:
function sort_comments_by_timestamp(&$comments, $prop)
{
usort($comments, function($a, $b) use ($prop) {
return $a->$prop < $b->$prop ? 1 : -1;
});
}
当时间戳相同时,如何按id 降序对 id 进行排序?
【问题讨论】:
-
有没有理由叫它
_by_timestamp,还要有$prop参数? -
@Matthew 除了
I was doing some tests和错误的命名?不 :) 顺便说一句已经修复了