# Sorting by complex function on panel pages

**URL:** <https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 26, 2019, 3:35pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379 "2019-05-26T15:35:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 26, 2019, 3:35pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/1 "2019-05-26T15:35:52Z")

</div>

Hello,  
On the frontend I am sorting $page-\>children() by using a ‘complex’ function as a custom page method, and I would like to sort the same pages in a pages section in the panel using the same or equivalent function which in pseudocode it goes like this:

```
create new pages collection
add all children who have field 'pinned' as 'true', sorted by field 'publicationDate' descending
add all children who have field 'pinned' as 'false', sorted by field 'publicationDate' descending
return the collection

```

According to the reference the sortBy option of pages section allows only for a field, and nothing else.

Could I extend the pages section in order to add a custom sorting option that uses my custom page method?

If yes, where can I find the pages section code ?

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:** [May 26, 2019, 3:48pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/2 "2019-05-26T15:48:53Z")

</div>

Your custom method should then do the job, no?

```auto
sortBy: customethodname

```

---

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 26, 2019, 3:51pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/3 "2019-05-26T15:51:55Z")

</div>

The method extends $page, so I call it as $page-\>customMethod()

You mean I can just do as you suggest on the pages section and it will use the actual page where the pages section is in place of $this? and then it will load the custom collection ?

```auto
sortBy: customethodname

```

---

<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:** [May 26, 2019, 3:57pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/4 "2019-05-26T15:57:00Z")

</div>

Yes, that’s what I meant

---

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 26, 2019, 4:02pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/5 "2019-05-26T16:02:44Z")

</div>

Does not seem to work.

This is the full blueprint at  
`site/pages/programme.yml`

I am using the method below the ‘updates’ tab and the ‘public’ pages section.

```auto
title: Programme

tabs:
  info:
    label: Info
    icon: text
    fields:
      info:
        type: textarea
        label: Info text
        size: large
        autofocus: false
        buttons:
            - link
            - email
      publicationDate:
        type: hidden

  updates:
    label: Updates
    icon: funnel
    columns:
      - width: 1/2
        sections:
          draft:
            type: pages
            headline: Draft
            text: "{{ page.intendedTemplate }} | {{ page.title }}"
            status: draft
            sortable: false
            sortBy: publicationDate
            templates:
              - announcement
              - event
              - call
              - article
      - width: 1/2
        sections:
          public:
            type: pages
            headline: Public
            status: listed
            sortBy: sortedUpdates
            text: "{{ page.intendedTemplate }} | {{ page.title }}

```

And the custom method at  
`site/plugins/page-methods/index.php`

```auto
<?php

Kirby::plugin('my/page-methods', [
		'pageMethods' => [
				'sortedUpdates' => function () {
					$sortedUpdates = new Pages();
						if($this->children()->listed()->count() > 0) {
							$twoMonthsAgo = new DateTime();
							$twoMonthsAgo->modify('-2 month');
							$sortedUpdates = $this->children()->listed()->filterBy('pinned', 'true')->sortBy('publicationDate', 'desc');
							foreach($this->children()->listed()->filterBy('pinned', 'false')->sortBy('publicationDate', 'desc') as $c) {
									$dateIt = new DateTime($c->publicationDate());
									if($dateIt > $twoMonthsAgo ) {
										$sortedUpdates->append($c);
									}
							}		
						}
					return $sortedUpdates;
				}
		]
]);

```

This is a screenshot of the result at frontend and backend:

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/9/918ff374ce8809a158f0de9043b83f2726865876.png)

---

<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:** [May 26, 2019, 4:11pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/6 "2019-05-26T16:11:10Z")

</div>

Oh, alright, I didn’t look close enough. What you use here as a page method is not really a page method but more a pages method, so you can’t really sort by that. Your custom page method or model would have to return a value for each individual page that you can sort by.

Such a page model could look like this:

```auto
public function indexValue() {
  $collection = $this->sortedUpdates();
    return $collection->indexOf($this);
}

```

Then in blueprint:

```auto
sortBy: indexValue

```

---

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 26, 2019, 4:58pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/7 "2019-05-26T16:58:28Z")

</div>

Oh I see,  
in your example $this is each page that I am sorting, right?

Perhaps I could use my method on $this siblings, then find the index of $this in the produced collection?

```
<?php

Kirby::plugin('my/page-methods', [
		'pageMethods' => [
				'sortedUpdates' => function () {
					$sortedUpdates = new Pages();
						if($this->siblings()->listed()->count() > 0) {
							$twoMonthsAgo = new DateTime();
							$twoMonthsAgo->modify('-2 month');
							$sortedUpdates = $this->siblings()->listed()->filterBy('pinned', 'true')->sortBy('publicationDate', 'desc');
							foreach($this->siblings()->listed()->filterBy('pinned', 'false')->sortBy('publicationDate', 'desc') as $c) {
									$dateIt = new DateTime($c->publicationDate());
									if($dateIt > $twoMonthsAgo ) {
										$sortedUpdates->append($c);
									}
							}		
						}
					return $sortedUpdates->indexOf($this);
				}
		]
]);

```

Seems quite overkill to do all that for each page, tho… It would make more sense to have the $sortedUpdates collection created once when the sorting starts, and use it in each instance of the loop to find $this indexOf.

I can’t create it beforehand because the code uses DateTime to get the actual date and time to calculate $twoMonthsAgo.

This is probably not possible, I assume?

Thanks

---

<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:** [May 26, 2019, 5:00pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/8 "2019-05-26T17:00:35Z")

</div>

My example was meant as a page model, assuming your pages in the collection are using the same template.

```auto
class SomePage extends Page {

    public function indexValue() {
      $collection = $this->sortedUpdates();
        return $collection->indexOf($this);
    }
}

```

---

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 26, 2019, 5:12pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/9 "2019-05-26T17:12:10Z")

</div>

I can confirm using siblings() and indexOf() in the code I added to my last response does sort as expected.

So I understand your page model would extend the page object with a custom method only for the SomePage template.

I also see this allows me to not write the sortedUpdates() code again, and I understand I can also use it as `sortBy: indexValue`

But, this does process the same code, right? it processes sortedUpdates() once per loop, once per $this to be sorted.

And also, then I should call sortedUpdates() on the parent of $this right?

```
class SomePage extends Page {

    public function indexValue() {
      $collection = $this->parent()->sortedUpdates();
        return $collection->indexOf($this);
    }
}

```

Thanks

---

<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:** [May 26, 2019, 5:42pm UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/10 "2019-05-26T17:42:44Z")

</div>

I’d consider using a custom collection that you cache and update the cache when something changes. That way while you still have to use the collection to get the index of the page, it doesn’t have to be recreated each time which might slow down if you have a lot of pages.

---

<div class="post-metadata">

**Author:** ![plagasul](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/plagasul/32/4199_2.png) [@plagasul](https://forum.getkirby.com/u/plagasul)\
**Post date:** [May 27, 2019, 12:19am UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/11 "2019-05-27T00:19:32Z")

</div>

The problem, as I explain above is that I

> I can’t create [the collection] beforehand because the code uses DateTime to get the actual date and time to calculate $twoMonthsAgo.

The actual date and time at the moment of sorting, that is. Which is why I asked for a way to create the collection when sorting starts, and then use the collection in each loop for each $this indexOf. But that does not seem to be something possible.

I did not know that I could use custom methods with sortby in blueprints. Is this explained explicitly in the reference or guide ? Could you please direct me there so I can summarize a full answer?

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:** [May 27, 2019, 6:06am UTC](https://forum.getkirby.com/t/sorting-by-complex-function-on-panel-pages/14379/12 "2019-05-27T06:06:07Z")

</div>

> [@plagasul](#):
>
> I did not know that I could use custom methods with sortby in blueprints.

No, I don’t think so, but the query languages works basically in the same way as the API. Admittedly, blueprint queries are currently not as powerful as the API (will be better in 3.2), but the main functionality is the same.
