Cakephp 3 - sorting data for croatian language

When sorting data in Croatian, the sorting result is inaccurate.
This problem does not occur when sorting data in English.
The problem creates diakrytic signs: Š, Đ, Č, Ć, Ž.

//for example
$this->loadModel(‘BlogPosts’);
$blogPosts = $this->BlogPosts->find(‘all’)->order([‘BlogPosts.title’ => ‘ASC’])->toArray();
pr($blogPosts);

The sorting result is:

…[title] => Auris tristique vehicula urna idspe…
…[title] => Bauris rutrum dolor ligula sed euismod ante…
…[title] => Člass aptent taciti sociosqu ad litora torquent…
…[title] => Corbi imperdiet sollicitudin lacus ut bibe…
…[title] => Morbi a ipsum et lorem venenatis luctus…
…[title] => Pellentesque euismod ante nibh eu viverra…
…[title] => Rellentesque tortor dolor semper vitae elit et…
…[title] => Đonec semper orci nec egestas condime…

Sorting is incorrect. It should be:

…[title] => Auris tristique vehicula urna idspe…
…[title] => Bauris rutrum dolor ligula sed euismod ante…
…[title] => Corbi imperdiet sollicitudin lacus ut bibe…
…[title] => Člass aptent taciti sociosqu ad litora torquent…
…[title] => Đonec semper orci nec egestas condime…
…[title] => Morbi a ipsum et lorem venenatis luctus…
…[title] => Pellentesque euismod ante nibh eu viverra…
…[title] => Rellentesque tortor dolor semper vitae elit et…

Croatian alphabet:A, B, C, Č, Ć ,D, Dž, Đ, E, F, G, H, I, J, K, L, Lj, M, N, Nj, O, P, R, S, Š, T, U, V, Z, Ž
How to solve this problem?

Have you checked the result of the query: “select title from blogs_posts order by title asc” ? I think the problem is not from cakephp but from the sql server… You may need to select the proper collation for your table.

1 Like

I checked the query.You were right. It looks like a sql server has a problem. Collation of the ‘title’ column was ‘utf8_general_ci’. I’ve changed ‘collation’ in ‘utf8_croatian_ci’ and now everything works just fine with ->order([‘BlogPosts.title’ => ‘ASC’]). Unfortunately there is a problem with ‘Collections’ ->sortBy(‘title’,SORT_ASC,SORT_LOCALE_STRING); does not sort the data correctly.

You are setting the locale in your app?

Yes, I18n::locale(‘hrv’);

If you have ordered the data from your query why you need it again on collection if data is already ordered?

I never use both methods of sorting data. It does not make sense. In some situations it is necessary to use it ->sortBy(‘title’,SORT_ASC,SORT_LOCALE_STRING);