By sleitner on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
12.0.x
Introduced in version:
12.0.0
Issue links:
Description:
New sortByString and createSortCollator functions in Drupal\Component\Utility\SortArray to introduce a generic sort helper for language-dependent string comparisons using a "natural order" algorithm. If php intl extension is installed it uses Collator::compare(), fallback to strnatcasecmp()
Usage example:
$collator = SortArray::createSortCollator(\Drupal::service('language_manager')->getCurrentLanguage()->getId());
return uasort($list, function ($a, $b) use ($collator) {
return static::compare($a, $b, $collator);
});
public static function compare(ConfigEntityInterface $a, ConfigEntityInterface $b, ?\Collator $collator): int {
$a_weight = $a->weight ?? 0;
$b_weight = $b->weight ?? 0;
if ($a_weight == $b_weight) {
$a_label = $a->label() ?? '';
$b_label = $b->label() ?? '';
return SortArray::sortByString($a_label, $b_label, $collator);
}
return $a_weight <=> $b_weight;
}
Impacts:
Module developers