Apply sorting for Extbase ObjectStorage

I, andmanyothers, had the issue that it’s not possible to apply a default sorting for child objects of the type Tx_Extbase_Persistence_ObjectStorage with extbase. For exmpale, I happen to have a „course“ object which stores „event“ objects. The default getter would just

return $this->events;

without no possibility to define in what ordering the objects are returned.

Setting the default ordering in the event repository has no effects whatsoever. But the problem is actually pretty easy to solve. Just create an instance of the child object repository in the parent model and then you can use the default findBy methods. Lets say your parent object is called „Parent“ then you’d use findByParent($this->getUid());

In the child repository apply your sorting and do whatever you like: