sortBy() versus accented characters

Hello, I’m trying to sort pages by title with sortBy(), but I’m struggling with various accented characters. All pages with accented first letter are not sorted correctly, can it be solved?

Thanks!

There is already an issue on GitHub, unfortunately, it’s not solved yet.

@texnixe The issue linked above doesn’t apply to accented character sorting, but rather “Default logic for Date/Time/Datetime”.

So … nine years later, I’m running into this same issue using both Kirby 4.5 and 4.6

I’m creating an list of pages, sorted alphabetically by title and arranged in subsections, but accented characters (in words such as “Über”) are sorted after all the other characters.

The code I’m using is based on the Grouping collections examples found in the Cookbook.

// Sort child pages by title, then group alphabetically

<?php $alphabetise = $page->children()->sortBy('title', 'asc', SORT_STRING|SORT_FLAG_CASE)->group(fn ($item) => str::upper($item->title()->value()[0])); ?>

// Create alphabetical list of pages, with subheadings

<?php if($items && $items->count()): ?>
<nav class="submenu alphabetical">
	<?php foreach($alphabetise as $letter => $items): ?>
	<h3><?php echo str::upper($letter) ?></h3>
	<ul>
		<?php foreach($items as $item): ?>
		<li><a href="<?php echo $site->url() . '/' . $item ?>"><?php echo html($item->title()) ?></a></li>
		<?php endforeach ?>
	</ul>
	<?php endforeach ?>
</nav>
<?php endif ?>

Here’s what I’m seeing on my site:

If there was a way to “de-accent” character strings before the sorting process, that would be ideal.

Sort() or sortBy() also accepts a callback as argument, so instead of just sorting by title, your callback can returns an ascii version of the title. Having said that, this might not fit the sorting rules for each language.

If you need that, you might want to look into the Collator class: PHP: Collator - Manual, but I cannot tell from the top of my head how to combine that with Kirby’s sorting.

Ok, this gives me a couple of ideas to tinker with.

Thanks!

What about sorting by the page slug instead of the title to get around the accents problem?

1 Like

If the title and slug have not diverged could be an option, and if you don’t need locale specific sorting

Some of my slugs are “wordified” versions of the page title.

For example:

The “@net” title is sluggified as “atnet”.

Not sure if this would work for every instance, as the “@net” page is alphabetized under the symbol subheading on my index page, whereas using the “atnet” slug would alphabetize it under the “A” subheading.

Maybe that’s an ok solution.

Ok, I went ahead with sorting and grouping pages using the slug and I’m happy with the result.

Thanks for the suggestions, @scottboms and @texnixe!

2 Likes