XF 2.0 Sort relation

Nothing built-in, I don't think. The relation entities will be keyed by the column specified in the structure, but even if that was the column you wanted to sort by, I don't think it necessarily implies any sort of ordering. I wish collections had more syntactic sugar for sorting, but all they've got right now is sortByList(array $keys), which leaves you to do most of the actual sorting yourself.

It's kind of clunky, but you can convert the collection to an array, use the array utility to sort by a column, and then convert it back into a collection:
PHP:
$collection = $collection->toArray();
$collection = \XF\Util\Arr::columnSort($collection, 'column');
$collection = \XF::em()->getBasicCollection($collection);
 
Last edited:
@Jeremy P Ok I see one column sorting but what about multiple column sorting? Such as allowing me to sort by player, spec, and professions.
 
It's kind of clunky, but you can convert the collection to an array, use the array utility to sort by a column, and then convert it back into a collection:
PHP:
$collection = $collection->toArray();
$collection = \XF\Util\Arr::columnSort($collection, 'column');
$collection = \XF::em()->getBasicCollection($collection);
Where do we specify if we need it by asc or desc?

I mean we can pass a callback function as third argument to \XF\Util\Arr::columnSort($collection, 'column', 'callbackfunction') but where do we define this function? If I put in the public controller (same where it's called from), I'm getting errors.
 
Last edited:
Top Bottom