# Trouble with collections of subpages and sorting by structure

**URL:** <https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 16, 2023, 8:33am UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121 "2023-03-16T08:33:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrsBee](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/krsbee/32/16276_2.png) [@KrsBee](https://forum.getkirby.com/u/KrsBee)\
**Post date:** [March 16, 2023, 8:33am UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121/1 "2023-03-16T08:33:20Z")

</div>

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

```auto
<?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 ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 16, 2023, 12:12pm UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121/2 "2023-03-16T12:12:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KrsBee](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/krsbee/32/16276_2.png) [@KrsBee](https://forum.getkirby.com/u/KrsBee)\
**Post date:** [March 16, 2023, 12:33pm UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121/3 "2023-03-16T12:33:47Z")

</div>

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

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

```

in the record.yml

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

```

Thank you

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 16, 2023, 12:49pm UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121/4 "2023-03-16T12:49:12Z")

</div>

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.

```auto
$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();
}
```

---

<div class="post-metadata">

**Author:** ![KrsBee](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/krsbee/32/16276_2.png) [@KrsBee](https://forum.getkirby.com/u/KrsBee)\
**Post date:** [March 16, 2023, 1:45pm UTC](https://forum.getkirby.com/t/trouble-with-collections-of-subpages-and-sorting-by-structure/28121/5 "2023-03-16T13:45:04Z")

</div>

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?
