php sort object by field asc/desc

如何对对象进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//sort by
$sortValues = array(
'country'=>'ASC',
'orders_count'=>'DESC',
'order_total'=>'DESC',
'average_order_total' => 'DESC'
);
$sortBy = ‘order_total’;
$this->sortOnField($items,$sortBy,$sortValues[$sortBy]);

function sortOnField(&$objects, $on, $order = 'ASC') {
$comparer = ($order === 'DESC')
? "return -strnatcmp(\$a->getData('{$on}'),\$b->getData('{$on}'));"
: "return strnatcmp(\$a->getData('{$on}'),\$b->getData('{$on}'));";
usort($objects, create_function('$a,$b', $comparer));
}

坚持原创技术分享,您的支持将鼓励我继续创作!