Submenu: How do I always display the active page in 1st position?

I am using the “independent sub sub menu”.
I would like to not only highlight the active page with CSS, but also always display it in the first position.
What is the best way to do this?


// independent sub sub menu
$items = false;

// get the open item on the first level
if($root1 = $pages->findBy('isOpen', true)) {

  // get the open item on the second level
  if($root2 = $root1->children()->findOpen()) {

    // get visible children of the second level item
    $items = $root2->children()->listed();

// only show the menu if items are available
if($items and $items->isNotEmpty()):

    <?php foreach($items as $item): ?>
    <li><a<?php e($item->isOpen(), ' class="active"') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a></li>
    <?php endforeach ?>
<?php endif ?>

Similar to this here, minus the shuffle. And the first element is not the first but the open one. So find the open one using findBy(isOpen), exclude from the rest of the items and then prepend.

I had looked at the article shortly before and thought the example was cool. But I couldn’t seem to count to 2 to adapt this solution for myself.
A clear sign that :face_with_spiral_eyes: I sit in front of the computer too long, :palm_tree: I don’t go on vacation enough, :brain: my brain is as small as a pea. Or a combination of all 3 things :joy:

It works with this code. I would be grateful for your fine-tuning.

if ($root1 = $pages->findBy('isOpen', true)) {
if ($root2 = $root1->children()->findOpen()) {
$unopened = $root2->children()->listed()->not($root2->children()->findOpen());
$first = $root2->children()->findOpen();
$items = $unopened->prepend($first);