# How to sort by grouped field?

**URL:** <https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 30, 2020, 2:59pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582 "2020-12-30T14:59:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [December 30, 2020, 2:59pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/1 "2020-12-30T14:59:15Z")

</div>

I want to show a list of **articles** , grouped by year, where year is a date in the **festival** , where festival is related field of article.

With this:

```
<?php

$callback = function($p) {
  return $p->festival()->toPage()->date()->toDate('Y');
};

$groupedItems = page('press')->children()->listed()->group($callback);

foreach($groupedItems as $year => $itemsPerYear): ?>
    <h2><?= $year ?></h2>
    <ul>
      <?php foreach($itemsPerYear->flip() as $item) : ?>
      <li><a href="<?= $item->sourceurl() ?>"><?= $item->title() ?></a>, <?= $item->source() ?>, <?= $item->date()->toDate('Y-m-d') ?></li>
      <?php endforeach; ?>
    </ul>
<?php endforeach ?>

```

I am getting this sort:

 ![Vilnius Jazz Festival 2020 Spauda_2020-12-30_16-57](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/6/6aac3ccb86438d34253c862e578a5ca8da8523de.png)

tried something like `foreach($groupedItems->sortBy('$callback')`, but no result, also in this case year are converted to numbers 0, 1, 2…

---

<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:** [December 30, 2020, 3:38pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/2 "2020-12-30T15:38:54Z")

</div>

You have to sort the collection before grouping. You can use the same callback as for the grouping,

```auto
$groupedItems = page('press')->children()->listed()->sort($callback, 'desc')->group($callback);

```

---

<div class="post-metadata">

**Author:** ![flokosiol](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/flokosiol/32/468_2.png) [@flokosiol](https://forum.getkirby.com/u/flokosiol)\
**Post date:** [December 30, 2020, 3:41pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/3 "2020-12-30T15:41:48Z")

</div>

Alright … this is just a wild untested guess 😅

At first I would create a custom page method `festivalYear()` for the children of the press page.

```php
// site/models/article.php – assuming the page type is "article"

class ArticlePage extends Page {
  public function festivalYear() {
    return $this->festival()->toPage()->date()->toDate('Y');
  }
}

```

Afterwards you should be able to sort and group by this custom method like this:

```auto
$groupedItems = page('press')->children()->listed()->sortBy('festivalYear', 'asc')->group(function($item) {
    return $item->festivalYear();
});

```

I didn’t know about the `group()` method so far. Great learning for me!

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [December 30, 2020, 3:48pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/4 "2020-12-30T15:48:25Z")

</div>

What do you mean?

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [December 30, 2020, 3:48pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/5 "2020-12-30T15:48:53Z")

</div>

Thanks, tried it, but items are not grouped with this code.

---

<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:** [December 30, 2020, 3:49pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/6 "2020-12-30T15:49:26Z")

</div>

I already updated my post…

---

<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:** [December 30, 2020, 3:51pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/7 "2020-12-30T15:51:21Z")

</div>

> [@flokosiol](#):
>
> ```auto
> $groupedItems = page('press')->children()->listed()->sortBy('festivalYear', 'asc')->group(function($item) {
> return $item->festivalYear();
> });
> 
> ```

This should probably be (not tested)

```auto
$groupedItems = page('press')->children()->listed()->sortBy('festivalYear', 'asc')->groupBy('festivalYear');

```

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [December 30, 2020, 3:51pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/8 "2020-12-30T15:51:53Z")

</div>

Yes, great, thank you, it solved the problem 🙂

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [January 3, 2021, 7:36am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/9 "2021-01-03T07:36:52Z")

</div>

@texnixe also one more question, can’t figure out how to pass the value of child pages radio field to `$callback`

A bit more details about the case:  
There are festivals, each festival has performers as child pages. Each performer has category as radio value. So on festival page I need to show performers, grouped by their categories.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [January 3, 2021, 9:35am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/10 "2021-01-03T09:35:55Z")

</div>

```auto
$performers = page('yourFestivalPage')->children()->listed()->groupBy('yourPerformerCategoryField');

```

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [January 3, 2021, 10:00am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/11 "2021-01-03T10:00:22Z")

</div>

Thank you, ok, clear, so turns out there is no need to get value of radio option 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [January 3, 2021, 10:08am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/12 "2021-01-03T10:08:11Z")

</div>

In my example `yourPerformerCategoryField` would be the radio field

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [January 3, 2021, 10:20am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/13 "2021-01-03T10:20:12Z")

</div>

Yes, that’s clear, thanks. Only can’t understand how to print values of radio. As need to show titles of categories. Should be something like:

```
<?php
$field = $page->children()->blueprint()->field('performertype');
$value = $page->children()->performertype()->value();
?>

<?= $performertypeValue = $field['options'][$value][$kirby->language()->code()] ?? $value; ?>

```

But missing something.

I mean to show radio value here (now `$performer` shows machine names of radio):

```
<?php $groupedItems = $page->children()->listed()->groupBy('performertype'); ?>

  <?php foreach ($groupedItems as $performer => $itemsPerPerformer): ?>
  
    <h2 class="h1"><?= $performer ?></h2>
     ....
```

---

<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:** [January 3, 2021, 11:25am UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/14 "2021-01-03T11:25:39Z")

</div>

I this case it probably makes sense to set up a category map (i.e. array of key/ value pairs) in your config file or, since you seem to have a multilanguage site, in your translation files, otherwise you would have to get the blueprint of one specific page and get the text that matches the current value from that page’s blueprint.

> **[Custom language variables](https://getkirby.com/docs/guide/languages/custom-language-variables)**
>
> Learn how to define translatable variables, which can be used to translate fixed parts in your templates like buttons.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [January 3, 2021, 12:09pm UTC](https://forum.getkirby.com/t/how-to-sort-by-grouped-field/20582/15 "2021-01-03T12:09:06Z")

</div>

Yes, that seems the easiest solution, tried and it works, thank you 🙂
