我正在开发一个为客户进行多级排序(在排序内排序,因为缺乏更好的术语)的函数。假设我们有一个具有不同属性的对象列表,例如:
- name - 对象的名称
- 类型 - 对象类型
- 日期 - 某些日期属性
假设我想首先按时间顺序对列表进行排序,然后按对象类型排序,然后按字母顺序排序。我该怎么做呢?
目前我正在使用usort()传入我自己的比较函数,该函数会将上述属性转换为不同权重的整数;例如。如果主要排序是按日期排序,我将其转换为某个整数,将其乘以 1000,将下一层排序转换为整数(在本例中为类型),将其乘以 100,依此类推,然后将其全部相加一起来确定一个对象是<还是>另一个。
有更简单/优雅的解决方案吗?谢谢
编辑:澄清一下,是否有更好的方法来进行多级排序而不将所有内容都转换为“权重”?
最佳答案
基本上,您想要做的是使用一系列“短路”比较。考虑到您的上述标准,一个简单的示例可能看起来像这样(未经测试):
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;
}
现在,添加或删除不同的排序字段或排序方向就像添加或修改数组元素一样简单。无需修改代码。
关于php - 进行多层排序的最佳/最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12539528/