Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

makapaka's avatar

Merged collection not sorting by date correctly

I am merging two collection objects and I can't get it sorted by a date field.

I've tried

$one->merge($two)->sortBy('kickoff')

Merged, but not sorting Also tried

$merged = $one->merge($two);
$sorted = $merged->sortBy(function($col) {
    return $col->kickoff;
})->values()->all();

Nothing seems to be sorting by the kickoff date as I expect.

kickoff is an attribute of format:

[kickoff] => 2018-07-07 19:30:00

Why doesn't laravel sortBy work ?

0 likes
1 reply
Snapey's avatar

Dump kickoff and check it is seeing the right value

$sorted = $merged->sortBy(function($col) {
    dump($col->kickoff);
    return $col->kickoff;
});

Please or to participate in this conversation.