# Sort collection by translated value from category map

**URL:** <https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728>\
**Category:** Questions\
**Tags:** v2\
**Created:** [October 7, 2017, 10:49pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728 "2017-10-07T22:49:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 7, 2017, 10:49pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/1 "2017-10-07T22:49:31Z")

</div>

So last question I gotten it all to work nicely. Now just missing if possible, sorting.

Is it a easy Kirby native way to [sortBy()](https://getkirby.com/docs/cheatsheet/pages/sort-by) the category using the final text? Also worth saying, the sort option works well, but I want sort on the final translated text value witch is stored in the config file sa an array.

May I ask for your input on this as well @texnixe?

Not sure if you follow me? Let me know if you need anything else of info?

---

<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:** [October 7, 2017, 11:02pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/2 "2017-10-07T23:02:13Z")

</div>

How do you fetch the final translated value?

If you convert that into a custom page method or page model, you can sort by that method.

E.g as a page model

```auto
class ProductPage extends Page {
  public function getCategoryTranslation() {
    return c::get('job_category_name')[$this->category()->value()];
  }
}

```

Then you can sort your groups by this method:

```auto
$categories = $groups->sortBy('getCategoryTranslation')->groupBy('category');

```

(hope, no missing parenthesis or the like anywhere…)

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 7, 2017, 11:12pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/3 "2017-10-07T23:12:24Z")

</div>

This is the code snippet:

In my snippet:

```auto
<?php $categories = $groups->groupBy('category');
    foreach($categories as $category => $items): ?>

<h1><?php echo $categoryName[trim($category)]; ?></h1>
// Rest of the loop...

```

In my template:

```auto
<?php $categoryName = c::get('job_category_name'); ?>

```

In the `config.php`

```auto
c::set('job_category_name', [
  'option1' => 'Some title 1',
  'option2' => 'Some title 2',
  'option3' => 'Some title 3'
]);

```

---

<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:** [October 7, 2017, 11:20pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/4 "2017-10-07T23:20:27Z")

</div>

Please see my edit above…

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 7, 2017, 11:31pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/5 "2017-10-07T23:31:47Z")

</div>

Did so but it did not work. No errors just no response.

Do I pass the value to the snippet correctly?

```auto
<?php snippet('overview', array('categoryName' => $categoryName, 'getCategoryTranslation' => $getCategoryTranslation)) ?>

```

Also is there a way to pass values from the snippet? Is that ever needed? (This is just good to know) 🙂

---

<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:** [October 7, 2017, 11:45pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/6 "2017-10-07T23:45:31Z")

</div>

There is no need to pass the page model method to the snippet.

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 7, 2017, 11:46pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/7 "2017-10-07T23:46:56Z")

</div>

Then I am a little confused since it did not work. 😕

---

<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:** [October 7, 2017, 11:49pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/8 "2017-10-07T23:49:07Z")

</div>

It’s a bit late now 😴, if you want, you can send me the relevant parts of your project as zip or upload to GitHub, then I could look into this tomorrow.

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 8, 2017, 12:43am UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/9 "2017-10-08T00:43:43Z")

</div>

I will… tomorrow. Time for 💤 here too. 😉

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 9, 2017, 1:00pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/10 "2017-10-09T13:00:05Z")

</div>

I am presented with a new issue, regarding the sort order. I wish to control the order, but after adding the links for filtering each category I am faced with another issue. So how can I add sort order as a extra field in the config file? (Not sure how to add that to the array).

Also how will you go around getting it sorted right in the front-end? I am thinking that the easy way around this is to use numbers just, `01, 02, 03` etc.

How you have a awesome week @texnixe 😄

---

<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:** [October 9, 2017, 1:17pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/11 "2017-10-09T13:17:42Z")

</div>

I’m not sure I get it.

So you don’t want to sort by alphabetical order of the “translated” strings (Some title 1, Some title 2) anymore but the order these values have in within the array?

> [@jonabaptistella](#):
>
> Also how will you go around getting it sorted right in the front-end? I am thinking that the easy way around this is to use numbers just, 01, 02, 03 etc.

If you use numbers for your options like this:

```auto
options:
  1: Option 1
  2: Option 2

```

you can, of course, use these to sort by and no need to sort by a custom page model/method.

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 9, 2017, 1:49pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/12 "2017-10-09T13:49:38Z")

</div>

I move away from the alphabetical sorting to a number based sorting. Here is my config file with the “translated” array. Now is there a way to attach another value to the each to give them the sorting order? Like`'sort' => 05` ?

```auto
// Job type names in the front-end :)
c::set('job_category_name', [
  'sand' => 'Sand, grus, gjørme',
  'fat' => 'Fett',
  'channels' => 'Åpene kanaler',
  'toilet' => 'Avløpsåpning',
  'debris' => 'Avleiringer',
  'cleaning' => 'Regelmessig rengjøring',
  'roots' => 'Røtter',
  'shaft' => 'Sjakt rengjøring',
  'objects' => 'Utstikkende objekter',
  'special' => 'Store og spesielle profiler'
]);

```

Do you follow me? Do I need an array inside the array?

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [October 9, 2017, 1:51pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/13 "2017-10-09T13:51:32Z")

</div>

Are you sure you do not mean “filter”? How can you sort something by “cleaning” as in your example?

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 9, 2017, 1:53pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/14 "2017-10-09T13:53:37Z")

</div>

> [@texnixe](#):
>
> you can, of course, use these to sort by and no need to sort by a custom page model/method.

I think this makes most sense. I will do this then. Cause then I do not add more issues then needed. 🙂

---

<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:** [October 9, 2017, 1:58pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/15 "2017-10-09T13:58:37Z")

</div>

```auto
c::set('job_category_name', [
  '1' => 'Sand, grus, gjørme',
  '2' => 'Fett',
  '3' => 'Åpene kanaler',
  '4' => 'Avløpsåpning',
  '5' => 'Avleiringer',
  '6' => 'Regelmessig rengjøring',
  '7' => 'Røtter',
  '8' => 'Sjakt rengjøring',
  '9' => 'Utstikkende objekter',
  '10' => 'Store og spesielle profiler'
]);

```

---

<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:** [October 9, 2017, 2:01pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/16 "2017-10-09T14:01:10Z")

</div>

> [@bvdputte](#):
>
> Are you sure you do not mean “filter”? How can you sort something by “cleaning” as in your example?

The idea here is to group with a certain sort order of the categories, not filter. The original idea was to sort alphabetically based on the values of the `c::set()` array.

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 9, 2017, 2:10pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/17 "2017-10-09T14:10:04Z")

</div>

Thank you @texnixe! 😊 Of course in a perfect world I will have named urls. Cause this is also the `param('1')` in the url. So that is why I was thinking of this as a separate value. But thank you!

Worth mentioning is that if ever want to reorder this you also need to resave all the pages with the new value since this is also reflected in my category select dropdown in the panel.

That is it would be great in a future version to add that. 🙂

---

<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:** [October 9, 2017, 2:16pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/18 "2017-10-09T14:16:45Z")

</div>

> [@jonabaptistella](#):
>
> Of course in a perfect world I will have named urls.

Oh, I see. Then maybe this quick and dirty solution is not so ideal, after all, and a multi-dimensional array with a sort order attached to it would be the more future-proof solution.

```auto
c::set('job_category_name', [
  'sand' => [ 'value' => 'Sand, grus, gjørme',
              'order' => 1
            ],
 //...
]);

```

---

<div class="post-metadata">

**Author:** ![jonabaptistella](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonabaptistella/32/5797_2.png) [@jonabaptistella](https://forum.getkirby.com/u/jonabaptistella)\
**Post date:** [October 9, 2017, 2:18pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/19 "2017-10-09T14:18:54Z")

</div>

Yes. You read my mind. 🙂 So how do I go around making the arrays and then how can I access/parse them in the front-end?

---

<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:** [October 9, 2017, 2:21pm UTC](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728/20 "2017-10-09T14:21:03Z")

</div>

Then you would need this wonderful page method/model again, with the difference that you now have to deal with a multi-dimension array to grab the correct sorting value…

> [@jonabaptistella](#):
>
> So how do I go around making the arrays

See above, I tend to have afterthoughts and then edit my answers to provide more detailed information.

And then I guess you want an adapted method as well… 🤔 But to enhance the learning effect, try it yourself first… and you will be proud of yourself if you get it to work 😉.

[Next page](https://forum.getkirby.com/t/sort-collection-by-translated-value-from-category-map/8728.md?page=2)
