# sortBy number and groups my projects

**URL:** <https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 19, 2021, 6:55pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995 "2021-11-19T18:55:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![QuentinJuhel](https://avatars.discourse-cdn.com/v4/letter/q/a587f6/32.png) [@QuentinJuhel](https://forum.getkirby.com/u/QuentinJuhel)\
**Post date:** [November 19, 2021, 6:55pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995/1 "2021-11-19T18:55:58Z")

</div>

Hi,

For a project I need to organize projects by years, the intention it’s to layout in the frontend my project like this :

> - 2021  
> project A  
> project B
> 
> - 2020  
> project C  
> project D

For the moment, I manage to sort my project by year and display the years. But I don"t really know how I can do the layout I want.

This is what I have for the moment :

> - 2021
> 
> - 2020
> 
> - project A
> 
> - project B
> 
> - project C
> 
> - project D

Here’s my code :

```auto
    <?php foreach ($pages->find('projets')->children()->pluck('year', ',', true) as $year): ?>
        <datetime><?= $year ?></datetime>
    <?php endforeach ?>

    <?php
      $articles = page('projets')->children()->listed()->sortBy(function ($page) {
        return $page->number()->toNumber();
      })->flip();
  
      foreach($articles as $article): ?>
        <!-- Looping through blocks, to get some blocks fields -->
        <?php foreach ($article->cover()->toBlocks() as $block): ?>

          <a class="column-<?= $block->layout() ?>" href="<?= $article->url() ?>">
            <h2><?= $article->number()->toNumber() ?></h2>
            <?= $block ?>
            <h3>
              <?= $article->title() ?>
            </h3>
          </a>
        <?php endforeach ?>
    <?php endforeach ?>

```

I was thinking by sorting byGroup. But I’m not really sure.  
If you have a suggestion, I’ll be glad to look at it.

Thank’s

---

<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:** [November 19, 2021, 6:59pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995/2 "2021-11-19T18:59:15Z")

</div>

Check out this cookbook recipe:

> **[Grouping collections](https://getkirby.com/docs/cookbook/content/grouping-collections)**
>
> Group collections like articles, events or files by various criteria.

---

<div class="post-metadata">

**Author:** ![QuentinJuhel](https://avatars.discourse-cdn.com/v4/letter/q/a587f6/32.png) [@QuentinJuhel](https://forum.getkirby.com/u/QuentinJuhel)\
**Post date:** [January 25, 2022, 1:26pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995/3 "2022-01-25T13:26:11Z")

</div>

Thanks !

I’ve got an other question.

I want also filter my projects by category. For the moment, it’s working, but the year when there is not projects in the selected category still displaying like this:

 ![Screenshot from 2022-01-25 14-21-27](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/2c778d2fdef6c61810e739c902602963937196c9.jpeg)

I don’t want to display the year 2019 because there no projects in the category « chercher » for this year.

Here is my code:

```auto
<?php
  $years = page('projets')->children()->listed()->sortBy('year')->groupBy('year');
  foreach($years->flip() as $year => $itemsPerYear): ?>
    <time><?= $year ?></time>
    <?php
    if($category = param('category')) {
         $itemsPerYear = $itemsPerYear->filterBy('category', param('category'), ',' );
       }
      foreach($itemsPerYear as $item) : ?>
       ....
    <?php endforeach ?>
<?php endforeach ?>

```

---

<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 25, 2022, 1:46pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995/4 "2022-01-25T13:46:11Z")

</div>

You have to move your category filter up:

```auto
<?php
  
$years = page('projets')->children()->listed()->sortBy('year', 'desc');
if($category = param('category')) {
  $years = $years->filterBy('category', $category, ',' );
}
$years = $years->groupBy('year');

foreach($years as $year => $itemsPerYear): ?>
    <time><?= $year ?></time>
    <?php foreach($itemsPerYear as $item) : ?>
       ....
    <?php endforeach ?>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![QuentinJuhel](https://avatars.discourse-cdn.com/v4/letter/q/a587f6/32.png) [@QuentinJuhel](https://forum.getkirby.com/u/QuentinJuhel)\
**Post date:** [January 25, 2022, 2:25pm UTC](https://forum.getkirby.com/t/sortby-number-and-groups-my-projects/23995/5 "2022-01-25T14:25:06Z")

</div>

Thanks, it was so simple !
