Skip to content

Commit 0a2d80f

Browse files
Fix issue #5542 Sorting by name
Replace <=> with Collator for better accented character handling Using <=> with strtolower() can misorder accented characters (e.g., "é" after "z"), making it less reliable for some languages. Collator from intl ensures precise, locale-aware string comparisons.
1 parent 0ec0913 commit 0a2d80f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

app/Sorting/SortSetOperationComparisons.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
1111
*/
1212
class SortSetOperationComparisons
1313
{
14+
protected static function getCollator(): ?\Collator
15+
{
16+
$locale = user()->getLocale()->isoLocale();
17+
return class_exists(\Collator::class) ? collator_create($locale) : null;
18+
}
19+
1420
public static function nameAsc(Entity $a, Entity $b): int
1521
{
22+
$collator = self::getCollator();
23+
if ($collator) {
24+
return $collator->compare($a->name, $b->name);
25+
}
1626
return strtolower($a->name) <=> strtolower($b->name);
1727
}
1828

1929
public static function nameDesc(Entity $a, Entity $b): int
2030
{
31+
$collator = self::getCollator();
32+
if ($collator) {
33+
return $collator->compare($b->name, $a->name);
34+
}
2135
return strtolower($b->name) <=> strtolower($a->name);
2236
}
2337

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"ext-dom": "*",
1414
"ext-fileinfo": "*",
1515
"ext-gd": "*",
16+
"ext-intl": "*",
1617
"ext-json": "*",
1718
"ext-mbstring": "*",
1819
"ext-xml": "*",

0 commit comments

Comments
 (0)