Content · Programming · PHP

PHP Collator::sort() Method: Usage and Examples

Understand Collator::sort() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to sort a list using locale-specific text ordering and reindex its elements. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The Collator::sort() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is Collator::sort() in PHP?

Sort a list using locale-specific text ordering and reindex its elements.

Its syntax is:

$collator->sort(array &$array, int $flags = Collator::SORT_REGULAR): bool

Parameters used by this operation:

  • &$array — The collection being examined or updated.
  • $flags — An optional flag that controls comparison or processing behavior.

Requires PHP intl and ICU data; original keys are lost when values are reindexed. Locale settings can affect the result.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$collator = new Collator("sv_SE");
$names = ["Östen", "Anders", "Åke"];
$collator->sort($names);
print_r($names);

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$collator = new Collator("en_US");
$words = ["zebra", "apple", "Banana"];
$collator->sort($words);
print_r($words);

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Practical Example: Order translated interface labels using the correct locale

Order translated interface labels using the correct locale. Save the code in a PHP file and adapt the sample data to your project.

<?php

$collator = new Collator("de_DE");
$productNames = ["Öl", "Apfel", "Zitrone"];
$collator->sort($productNames);
foreach ($productNames as $productName) { echo $productName . PHP_EOL; }

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Important Behavior and Edge Cases

Requires PHP intl and ICU data; original keys are lost when values are reindexed. Locale settings can affect the result.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
Collator::sort()Sort a list using locale-specific text ordering and reindex its elements
Related functionsCollator::asort() preserves original keys; sort() uses PHP comparison rules.

Common Beginner Mistakes

MistakeCorrect Approach
Confusing key sorting with value sortingCheck whether the comparison applies to keys or values before selecting the sort method.
Expecting an unchanged collectionThe sorting operation updates the array or collection in place; copy it first when you need the original.
Ignoring comparison rulesRequires PHP intl and ICU data; original keys are lost when values are reindexed. Locale settings can affect the result.

Key Takeaways

  • Collator::sort() helps you sort a list using locale-specific text ordering and reindex its elements.
  • Requires PHP intl and ICU data; original keys are lost when values are reindexed. Locale settings can affect the result.
  • Collator::asort() preserves original keys; sort() uses PHP comparison rules.
  • Use the practical example as a starting point, but validate real application inputs.

Collator::sort() is useful when you need to sort a list using locale-specific text ordering and reindex its elements. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.