Trouble with collections of subpages and sorting by structure

Hello, I’m still having trouble with collections of subpages and sorting, but it tart to make more sense…
Here is my problem: I have a project that catch labels from the page records/record. The labels is a structure. here is how I do, but it sort the list by record and I want to sort it alphabetically by labels not records. Can you help me, thank you

<?php if ($discography = $page->grandchildren()->filterBy('template','record')): ?>
    <?php foreach ($discography as $record): ?>
        <?php if ($record->labels()->isNotEmpty()): ?>
            <ul>
                <?php
                $labels = $record->labels()->toStructure();
                $sortedLabels = $labels->sortBy(function ($page) {
                return $page->text();
                }, 'desc');
                foreach($sortedLabels as $label): ?>
                    <li>                                                          
                        <a href="<?= $label->url() ?>" target="_blank">
                        <?= $label->text() ?>
                        </a>
                    </li>
                <?php endforeach ?>
            </ul>
        <?php endif ?>
    <?php endforeach ?>
<?php endif ?>

Don’t understand the issue, this is only about sorting the $record->labels() right. But it would be helpful if you post the blueprint for the labels structure field.

Hello texnixe, sorry for my confusion.
My difficulty is how to call pages that are pages in pages and then to create a collection with a field, in this example from the structure. I try to have a collection of all the labels and sort them alphabeticaly, and not of the labels (alphabetically sorted) sort by record collection.
here is my blueprint for the labels section

label: Label(s)
type: structure
fields:
  text:
    label: Nom du label
    type: text
  url:
    label: URL
    type: url

in the record.yml

columns:
  - width: 1/2
    sections:
      info:
        type: fields
        fields:
          labels: 
            extends: sections/labels 

Thank you

Just to make sure I understand this correctly, you want to fetch all the structure fields of all the grandchildren into one labels collection that you sort alphabetically? If that’s the case, your approach above doesn’t make sense.

$lableFields = $page->grandchildren()->filterBy('template','record')->pluck('labels');
$combinedStructure = new Kirby\Cms\Structure();
foreach ($lableFields as $labelField) {
  $combinedStructure->add($labelField->toStructure());
}

$combinedStructure = $combinedStructure->sortBy('text', 'desc');

foreach($combinedStructure as $label) {
  echo $label->text();
}

Thank you! I don’t understand what you do, but I will look up in the documentation, because it is so precise.
I still have a question: with this method is there a way to exclude when the label is doubled from two differents records?