Sortby date when datefield is in an object

I’m filtering events as shown below in which the filtering works but the sorting (by startDate) is not working. I think it’s because startDate is inside an object. How can I make this work?

$archive = page('page://YQ74DX24BOLNDBOE')
->children()
->filterBy(function ($callback) {
    return $callback->generalInfo()->toObject()->startDate()->toDate('ymd') < date('ymd') && $callback->generalInfo()->toObject()->closeDate()->toDate('ymd') < date('ymd');
})
->sortBy('startDate', 'asc')

Instead of a string, you can use a function as the first argument of the sortBy method.

$archive = page('page://YQ74DX24BOLNDBOE')
->children()
->filterBy(function ($item) {
    return $item->generalInfo()->toObject()->startDate()->toDate('ymd') < date('ymd') && $item->generalInfo()->toObject()->closeDate()->toDate('ymd') < date('ymd');
})
->sortBy(function ($item) {
  return $item->generalInfo()->toObject()->startDate()->toDate('ymd');
}, 'asc')

More Information on sorting:

1 Like