我正在开发一个函数来为客户端进行多级排序(在一种排序中排序,缺少更好的术语)。假设我们有一个具有不同属性的对象列表,例如:
假设我想先按时间顺序排序列表,然后按对象类型排序,然后按字母顺序排序。我该怎么做呢?
目前我正在使用usort()来传递我自己的比较函数,它将上述属性转换为具有不同权重的整数;例如。如果主要排序是按日期,我将其转换为某个整数,将其乘以1000,将下一层排序转换为整数(在本例中为类型),将其乘以100,依此类推,然后将其全部添加一起确定对象是否是<或者>另一个。
是否有更简单/更优雅的解决方案?感谢
编辑:澄清一下,有没有更好的方法来进行多级排序而不将所有内容都转换为'权重'?
答案 0 :(得分:4)
基本上,您要做的是使用一系列“短路”比较。根据您的上述标准,一个简单的例子可能看起来像这样(未经测试):
function mySort($a, $b) {
if ($a->name < $b->name) {
return -1;
}
if ($a->name > $b->name) {
return 1;
}
// If we get this far, then name is equal, so
// move on to checking type:
if ($a->type < $b->type) {
return -1;
}
if ($a->type > $b->type) {
return 1;
}
// If we get this far, then both name and type are equal,
// so move on to checking date:
if ($a->date < $b->date) {
return -1;
}
if ($a->date > $b->date) {
return 1;
}
// If we get this far, then all three criteria are equal,
// so for sorting purposes, these objects are considered equal.
return 0;
}
正如我所说,这是一个天真的解决方案,而且它是非常不可扩展的。我建议使用稍微更健壮的解决方案,其中您的排序不会硬编码到排序方法中。采用这种方法,例如(未经测试):
// These are the properties to sort by, and the sort directions.
// They use PHP's native SORT_ASC and SORT_DESC constants.
$this->_sorts = [
'name' => SORT_ASC,
'type' => SORT_ASC,
'date' => SORT_ASC
];
// Implemented as a class method this time.
protected function _mySort($a, $b) {
foreach ($this->_sorts as $property => $direction) {
if ($a->{$property} < $b->{$property}) {
return $direction === SORT_ASC ? -1 : 1;
}
if ($a->{$property} > $b->{$property}) {
return $direction === SORT_ASC ? 1 : -1;
}
}
return 0;
}
现在,添加或删除不同的排序字段或排序方向就像添加或修改数组元素一样简单。无需修改代码。