2

我正在开发一个功能来为客户进行多级排序(在一个排序中排序,因为没有更好的术语)。假设我们有一个具有不同属性的对象列表,例如:

  • name - 对象的名称
  • 类型 - 对象类型
  • date - 一些日期属性

假设我想先按时间顺序对列表进行排序,然后按对象类型,然后按字母顺序。我该怎么做呢?

目前我正在使用 usort() 传入我自己的比较函数,它将上述属性转换为不同权重的整数;例如。如果主要排序是按日期,我​​将其转换为某个整数,将其乘以 1000,将下一层排序转换为整数(在本例中为类型),将其乘以 100,依此类推,然后将其全部相加一起来确定一个对象是 < 还是 > 另一个。

有更简单/优雅的解决方案吗?谢谢

编辑:澄清一下,有没有更好的方法来进行多级排序而不将所有内容都转换为“权重”?

4

1 回答 1

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;
}

现在,添加或删除不同的排序字段或排序方向就像添加或修改数组元素一样简单。无需修改代码。

于 2012-09-22T03:54:53.313 回答