# How to sort paginator output in Cell?

**URL:** <https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243>\
**Category:** Need Help\
**Created:** [April 14, 2021, 2:43pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243 "2021-04-14T14:43:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [April 14, 2021, 2:43pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/1 "2021-04-14T14:43:53Z")

</div>

Hello,

I would like to ask kindly someone to help me with this issue in my CakePHP 4.2.4 web app.

I have public function display() in my MenuCell class, which fetches data from database Menus (id, link\_name, controlle, action, date, date\_modified). Based on the database Table, I want to generate side navigation links in ascending order, i.e.

Menu

A …  
B …  
C …  
D …  
E …

I paginate data from the database as follows:

```
public function display()
{
    $this->loadModel('Menus');
	
    // Create a paginator
    $paginator = new Paginator();
	$results = $paginator->paginate($this->Menus, ['order' => ['Menus.name' => 'asc']]);
    $this->set('menuitems', $results);
}

```

Nevertheless, result is that variable $menuitems is sorted according to the id key in database table, i.e. my Menu is ordered as follows:

E …  
A …  
B …  
D …  
C …

I tried to add parameters into paginator,

$results = $paginator-\>paginate($this-\>Menus, [‘order’ =\> [‘Menus.name’ =\> ‘asc’]]);

I am sure that solution is trivial but I have almost no experience with OOP and can not find the solution.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 14, 2021, 2:57pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/2 "2021-04-14T14:57:00Z")

</div>

[https://book.cakephp.org/4/en/controllers/components/pagination.html#advanced-usage](https://book.cakephp.org/4/en/controllers/components/pagination.html#advanced-usage)

Not tested, but something like this should work:

> public $paginate = [  
> ‘Menus’ =\> [  
> ‘order’ =\> [  
> ‘Menus.name’ =\> ‘asc’],
> 
> ```
> ];
> 
> ```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 14, 2021, 3:19pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/3 "2021-04-14T15:19:07Z")

</div>

You want to create menus that contain only a subset of the menu options?

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [April 17, 2021, 12:36pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/4 "2021-04-17T12:36:26Z")

</div>

Thank you dirk for your reply and thank you Zuluru for asking for additional information. Maybe my question will be clearer after reading my attempt to fix the issue below.

I tested to set $public variable according to dirk answer but this approach works only when I call the view using menus controller, i.e. /myapp/menus/sidemenuview/ When I try to sort items of the list in a Cell, it does not work. Here is the output as well as source code for comparison:

 ![4](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/3f11bd177cb9216ed5eeacd3d3834f06a2621f29.png)

Here are Cell definition and MenusController

 ![7](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/b2e7f121f62dec529acc3af2f5c8818b40adb586.png)

View file /templates/Menus/sidemenuview.php and /templates/cell/Menu/display.php are identical:

 ![6](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/6/6fb85dc9339a8cc5e43c66d1083d7f5e25afab87.png)

Maybe, its a bug or maybe parameters of the $public variable are transfered via URL somehow and this is the cause, why the sorting does not work for Cell. Please could someone chek this?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 17, 2021, 2:36pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/5 "2021-04-17T14:36:59Z")

</div>

I’ll ask this again. You want to create menus that contain only a subset of the menu options that are in the database?

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [April 17, 2021, 3:13pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/6 "2021-04-17T15:13:31Z")

</div>

Thank you for your interest, Zuluru. Answer is no, the database with menu items is made manually and I want to fetch all data from second, third and fourth column. Nevertheless, I found solution, its in the cakephp Cookbook.

For everyone with similar problem, the solution is in images below (i slightly modified font in sidemenuview.php, so it looks differently, view files are attached):

MenuController and MenuCell files:

 ![11](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/bc35a91c84eadd8ca95349b911c07f0d4f82a53e.png)

View files:

 ![33](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/4c2a04285c86369cc1d2a7fd3f2da969f20abf57.png)

Output:

 ![22](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/d/db14b00e8a747ea1d315221169873cbd334aa780.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 17, 2021, 4:01pm UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/7 "2021-04-17T16:01:10Z")

</div>

Where I was going with my question is that, if you want all the menu options to be loaded, then the paginator is not something you should be using at all. You should just do a `find` and get all the results.

---

<div class="post-metadata">

**Author:** ![user\_looser](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/user_looser/32/2161_2.png) [@user\_looser](https://discourse.cakephp.org/u/user_looser)\
**Post date:** [April 18, 2021, 9:20am UTC](https://discourse.cakephp.org/t/how-to-sort-paginator-output-in-cell/9243/8 "2021-04-18T09:20:18Z")

</div>

OK, I will try to use find() method and if I success, I will paste solution here.
