# Sorting in a multi-language context when a client can add their own languages

**URL:** <https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470>\
**Category:** Questions\
**Tags:** v3\
**Created:** [September 13, 2023, 2:46pm UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470 "2023-09-13T14:46:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 13, 2023, 2:46pm UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470/1 "2023-09-13T14:46:32Z")

</div>

I built a multi-language site that has a Glossary page. On the English page the entries are alphabetised:

> **[Glossary | IFB International Freightbridge](https://ifb.com/en/resources/glossary)**
>
> Find important logistics terms and look up their definition. Dictionary, freight forwarder words, abbreviations, logistics learning.

There was an issue with Alphabetising Chinese so I used a simple check to turn of Alphabetisation when non-latin characters are detected in the page title:

```auto
  <?php if(ctype_alpha($page->title()->toString())): ?>

```

However, this is picking up the diacritics of Polish an non-latin. The client is now asking if they can have alphabetisation in Polish but characters like ‘Ł’ are not being sorting (they want it near L, whatever I try it’s being sorted at the end).

I’ve read about setting the locale via the config, but I’d have to do this manually and the client needs to option to have new languages themselves via the panel. I’m a bit stumped on how to tackle this one, what would be the best approach? I tried setting the locale like this:

```auto
  setlocale(LC_ALL, $kirby->language()->locale(LC_ALL));

```

But it doesn’t work. Any ideas?

---

<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:** [September 13, 2023, 5:18pm UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470/2 "2023-09-13T17:18:01Z")

</div>

Setting the locale separately shouldn’t be necessary, the locale is set in the language config files.

However, I found that even the sorting flag on `sortBy()` doesn’t work properly for the Polish language (`	$projects = $page->children()->listed()->sortBy('title', 'asc', SORT_LOCALE_STRING);`), not does even a simple array sort with the sorting flag

```auto
$titles = $projects->pluck('title', ',');
asort($titles, SORT_LOCALE_STRING);
dump($titles);

```

I googled a bit and found this:

```auto
$titles = $projects->pluck('title', ',');
$coll = collator_create($kirby->language()->locale()[0]); 
collator_asort($coll, $titles);
dump($titles);

```

which seems to sort the titles correctly.

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 14, 2023, 10:47am UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470/3 "2023-09-14T10:47:31Z")

</div>

Thanks! This works great when plucking the titles, but I need to return a page object to get the URL:

```auto
    <?php
    $alphabetise = $page->children()->listed();
    $coll = collator_create($kirby->language()->locale()[0]); 
    collator_asort($coll, $alphabetise); // doesn't work because $alphabetise is not an array
    ?>
    <ul class="glossary__terms">
    <?php foreach($alphabetise as $item): ?>
      <li>
        <a href="<?= $item->url()?>"><?= $item->title()?></a>
      </li>
    <?php endforeach ?>
    </ul>
  

```

I thought about sorting titles and urls into two separate arrays but they wouldn’t be in the same order after` collator_asort`

---

<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:** [September 14, 2023, 10:54am UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470/4 "2023-09-14T10:54:39Z")

</div>

> [@jamiehunter](#):
>
> but I need to return a page object to get the URL:

Yes, I’m aware of that.

Unfortunately, I have no idea whatsoever, why the sorting doesn’t work properly with the sort flag set (not even in basic PHP).

So why I posted my findings was to find a workaround, i.e. using the collator result for custom sorting via mapping of the collection, similar to what I did in this cookbook recipe:

> **[Sorting](https://getkirby.com/docs/cookbook/content/sorting#custom-sorting)**
>
> Use Kirby's API to sort your content by all sorts of criteria.

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 14, 2023, 11:06am UTC](https://forum.getkirby.com/t/sorting-in-a-multi-language-context-when-a-client-can-add-their-own-languages/29470/5 "2023-09-14T11:06:38Z")

</div>

This may be crude, but sorting the titles into an array then finding the page by title in the loop seems to work…

```auto
    <?php
    $alphabetise = $page->children()->listed()->pluck('title',',');
    $coll = collator_create($kirby->language()->locale()[0]); 
    collator_asort($coll, $alphabetise);
    ?>
    <ul class="glossary__terms">
    <?php foreach($alphabetise as $item):
    $target = $page->children()->listed()->findBy('title',$item)
    ?>
      <li>
        <a class="arrow-link" href="<?= $target->url()?>"><?= $target->title()?></a>
      </li>
    <?php endforeach ?>

```

This seems to work for the Chinese version of the page too, and I have no idea how Chinese sorting works! Thanks for pointing me in the right direction @texnixe , you’re the best!
