sortBy() a language specific field value

Hello everyone, I have a question regarding the smartest use of sortBy(). I’m trying to sort a Pages Collection by the value of a specific field (say the fields name is ranking).

$sorted_children = $page->children()->sortBy('ranking', 'desc')

So far, so easy. On some pages though, the ranking field has different values for my two languages. I always want to sort the Pages Collection by the value of the field in English and ignore the German value.

As far as I understand it, the sortBy() function does not have an option to query the value for a different language than the currently set one. Is that right?

Ideally I would like a solution that looks something like this

// sort by the english value of ranking field
$sorted_children = $page->children()->sortBy($lang='en', 'ranking', 'desc')

The only way I see right now would work like this

$sorted_children = $page->children()->sortBy(function ($page) {
  return $page->content('en')->ranking();
}, 'desc');

This would add quite a lot of code though and I’m wondering if there’s a more elegant solution.

That is in fact the only way, be you can shorten it:

$sorted_children = $page->children()->sortBy(fn ($page) => $page->content('en')->ranking(), 'desc');

Or you create a custom sort pages function to hide the logic

Great! The short version you suggested is already short enough for me to not feel messy. Thanks!