0

让我们有一个初始数组:

$hits = [
    ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 1],
    ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 4],
    ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 2],
    ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 1],
    ['date' => new \DateTime('2019-01-01 12:00:00'), 'roundIndex' => 1],
    ['date' => new \DateTime('2019-01-04 12:00:00'), 'roundIndex' => 4],
    ['date' => new \DateTime('2019-01-02 12:00:00'), 'roundIndex' => 2],
    ['date' => new \DateTime('2019-01-23 12:00:00'), 'roundIndex' => 1],
    ['date' => new \DateTime('2019-01-22 12:00:00'), 'roundIndex' => 6],
    ['date' => new \DateTime('2019-01-01 12:00:00'), 'roundIndex' => 3],
    ['date' => new \DateTime('2019-01-06 12:00:00'), 'roundIndex' => 2],
    ['date' => new \DateTime('2019-01-28 12:00:00'), 'roundIndex' => 7],
    ['date' => new \DateTime('2019-01-26 12:00:00'), 'roundIndex' => 3],
    ['date' => new \DateTime('2019-01-27 12:00:00'), 'roundIndex' => 5],
    ['date' => new \DateTime('2019-01-26 12:00:00'), 'roundIndex' => 2],
    ['date' => new \DateTime('2019-01-11 12:00:00'), 'roundIndex' => 4],
    ['date' => new \DateTime('2019-01-24 12:00:00'), 'roundIndex' => 3],
    ['date' => new \DateTime('2019-01-08 12:00:00'), 'roundIndex' => 7],
    ['date' => new \DateTime('2019-01-11 12:00:00'), 'roundIndex' => 8],
    ['date' => new \DateTime('2019-01-14 12:00:00'), 'roundIndex' => 6],
    ['date' => new \DateTime('2019-01-13 12:00:00'), 'roundIndex' => 4],
    ['date' => new \DateTime('2019-01-13 12:00:00'), 'roundIndex' => 5],
    ['date' => new \DateTime('2019-01-24 12:00:00'), 'roundIndex' => 2],
    ['date' => new \DateTime('2019-01-27 12:00:00'), 'roundIndex' => 4],
];

我想按date排序,如果日期相等,按roundIndex 排序。考虑到“roundIndex”可能不存在。

第一的:

usort($hits, static function(array $item1, array $item2) {
    return $item1['date'] < $item2['date'] ? -1 : 1;
});

结果是:

2019-01-01 12:00:00 1
2019-01-01 12:00:00 3
2019-01-02 12:00:00 4
2019-01-02 12:00:00 2
2019-01-02 12:00:00 1
2019-01-02 12:00:00 2
2019-01-02 12:00:00 1
2019-01-04 12:00:00 4
2019-01-06 12:00:00 2
2019-01-08 12:00:00 7
2019-01-11 12:00:00 8
2019-01-11 12:00:00 4
2019-01-13 12:00:00 5
2019-01-13 12:00:00 4
2019-01-14 12:00:00 6
2019-01-22 12:00:00 6
2019-01-23 12:00:00 1
2019-01-24 12:00:00 3
2019-01-24 12:00:00 2
2019-01-26 12:00:00 2
2019-01-26 12:00:00 3
2019-01-27 12:00:00 4
2019-01-27 12:00:00 5
2019-01-28 12:00:00 7

到目前为止,一切都很好。所以第二次排序:

usort($hits, function($a,$b) use ($defaults) {
    if (
            $a['date']->format('Y-m-d H:i:s') === $b['date']->format('Y-m-d H:i:s'))
        {
            if ($a['roundIndex'] > $b['roundIndex'])
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        return 0;
    });

结果现在有点搞砸了:

2019-01-08 12:00:00 7
2019-01-04 12:00:00 4
2019-01-06 12:00:00 2
2019-01-02 12:00:00 1
2019-01-02 12:00:00 1
2019-01-02 12:00:00 2
2019-01-02 12:00:00 2
2019-01-02 12:00:00 4
2019-01-11 12:00:00 4
2019-01-11 12:00:00 8
2019-01-27 12:00:00 4
2019-01-26 12:00:00 3
2019-01-24 12:00:00 2
2019-01-26 12:00:00 2
2019-01-23 12:00:00 1
2019-01-24 12:00:00 3
2019-01-22 12:00:00 6
2019-01-14 12:00:00 6
2019-01-13 12:00:00 4
2019-01-13 12:00:00 5
2019-01-01 12:00:00 3
2019-01-27 12:00:00 5
2019-01-28 12:00:00 7
2019-01-01 12:00:00 1

我真的不明白,为什么。但如果我手动订购:

$sizeOfHits = sizeof($hits);
for($i = 0; $i < $sizeOfHits; $i++)
{
    for($j = $i+1; $j < $sizeOfHits; $j++)
    {
        if (
            $hits[$i]['date']->format('Y-m-d H:i:s') === $hits[$j]['date']->format('Y-m-d H:i:s'))
        {
            if ($hits[$i]['roundIndex'] > $hits[$j]['roundIndex'])
            {
                $x = $hits[$j];
                $hits[$j] = $hits[$i];
                $hits[$i] = $x;
            }
        }
    }
}

所有的结果都是完美的!但是会有10000个元素,测得手动点单需要20-30秒……

在这里你可以试试这个:https ://ideone.com/ZaV5Ar

4

2 回答 2

1

请原谅非常简洁的解决方案,但它基本上是使用宇宙飞船运算符 ( <=>) 比较日期,如果比较日期的结果是错误的(即它们相等),那么它会返回 roundIndex 值的比较......

usort($hits, function($a,$b) {
    return $a['date'] <=> $b['date'] ?: 
         ($a['roundIndex']??0) <=> ($b['roundIndex']??0);
});
于 2020-04-12T11:47:07.337 回答
1

我想这已经足够了:

usort($hits, function($a, $b) {
   // You can compare DateTime objects directly
   if ($a['date'] == $b['date']) {
       // use ?? to check if `roundIndex` exists
       return ($a['roundIndex'] ?? 0) - ($b['roundIndex'] ?? 0);
   } 
   return $a['date'] > $b['date'];
});
于 2020-04-12T11:43:11.120 回答