我有以下数组:
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 进行排序?
一个带有$props的数组
function sort_comments_by_timestamp(&$comments, $props)
{
usort($comments, function($a, $b) use ($props) {
if($a->$props[0] == $b->$props[0])
return $a->$props[1] < $b->$props[1] ? 1 : -1;
return $a->$props[0] < $b->$props[0] ? 1 : -1;
});
}
然后用
sort_comments_by_timestamp($unsorted_array,array("timestamp","id"));
如果您希望它与 X 个$props一起使用,您可以在 usort 中创建一个循环,始终将属性与其在数组中的前一个属性进行比较,如下所示:
function sort_comments_by_timestamp(&$comments, $props)
{
usort($comments, function($a, $b) use ($props) {
for($i = 1; $i < count($props); $i++) {
if($a->$props[$i-1] == $b->$props[$i-1])
return $a->$props[$i] < $b->$props[$i] ? 1 : -1;
}
return $a->$props[0] < $b->$props[0] ? 1 : -1;
});
}
干杯!
function sort_comments_by_timestamp(&$comments, $prop)
{
usort($comments, function($a, $b) use ($prop) {
if ($a->$prop == $b->$prop)
return $b->id - $a->id;
else
return $a->$prop < $b->$prop ? 1 : -1;
});
}
上面的参数首先按$prop
参数排序,然后按id
进行辅助排序。
用茴香酒来做(我知道你:P听说过(。
$result = Arrays::sort($array,
Comparator::compound(
Comparator::compareBy('timestamp'),
Comparator::compareBy('id')
)
);
这是一个相当古老的问题,但是当您想像mySQL ORDERBY那样按多个属性排序时,没有找到太多信息 这是来自个人框架的功能
选项 1:order($key, $direction='asc'(,其中 $key 是对象的属性,$direction 是 'asc' 或 'desc'
选项 2:订单(数组($key => $direction, $key => $direction((这类似于选项 1,但是当 2 个对象具有相同的$key值时,将使用传递的数组中的第二个排序选项。
public function order($arr, $key=null, $direction='ASC'){
if(!is_string($key) && !is_array($key))
throw new InvalidArgumentException("order() expects the first parameter to be a valid key or array");
$props = array();
if(is_string($key)) {
$props[$key] = strtolower($direction) == 'asc' ? 1 : -1;
}else{
$i = count($key);
foreach($key as $k => $dir){
$props[$k] = strtolower($dir) == 'asc' ? $i : -($i);
$i--;
}
}
usort($arr, function($a, $b) use ($props){
foreach( $props as $key => $val ){
if( $a->$key == $b->$key ) continue;
return $a->$key > $b->$key ? $val : -($val);
}
return 0;
});
return $arr;
}
$result = -1;
if ($a->timestamp < $b->timestamp) {
$result = 1;
} else if ($a->timestamp === $b->timestamp) {
if ($a->id < $b->id) $result = 1;
}
return $result;
把它放在 usort 闭包中。您还可以摆脱$prop参数和use ($prop)
部分。
function compare_city($a, $b)
{
// sort by state
$retval = strnatcmp($a->state, $b->state);
// if identical, sort by city
if(!$retval) $retval = strnatcmp($a->city, $b->city);
return $retval;
}
// sort alphabetically by state and city
usort($sortable, __NAMESPACE__ . ''compare_city');
这个功能对我有用!答案位于: 排序简单 XMLElement 对象数组
接受的答案有一个循环变体,并不总是有效。
if($a->$props[$i-1] == $b->$props[$i-1])
return $a->$props[$i] < $b->$props[$i] ? 1 : -1;
OBJ1 Prop1:foo, prop2: bar, prop3: test
OBJ2 Prop1:foo, prop2: bar, prop3: apple
在 $i = 1 时,第一次比较为真,由于 bar 不小于 bar,它将返回 -1,而不是继续循环来评估 prop3。
仅供参考。
我有一个也接受方向的变体,因此每个属性也可以分别使用 -1 和 1 按升序或降序排序。
function sortByProps(&$anArray, $props) {
usort($anArray, function($a, $b) use ($props) {
for($i = 0; $i < count($props); $i++) {
if( $a->{$props[$i][0]} < $b->{$props[$i][0]}) {
return $props[$i][1];
}
else if( $a->{$props[$i][0]} > $b->{$props[$i][0]}) {
return -1* $props[$i][1];
}
}
return 0;
});
}
sortByProps($someArray,(array(['name',-1],['address',-1],['occupation',-1],['favorite_food',-1])));
下面介绍如何按任意数量的条件进行排序,一个接一个地打破联系。它的工作方式很像 sql order by col1, col2
子句。
我总是忘记$a - $b
与$b - $a
排序是上升还是下降。根据需要进行调整。
$comparatorSequence = array(
function($a, $b) { return $a->timestamp - $b->timestamp; }
, function($a, $b) { return $a->id - $b->id; }
);
usort($theArray, function($a, $b) use ($comparatorSequence) {
foreach ($comparatorSequence as $cmpFn) {
$diff = call_user_func($cmpFn, $a, $b);
if ($diff !== 0) {
return $diff;
}
}
return 0;
});