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 Approach | When to Use It |
|---|---|
Collator::sort() | Sort a list using locale-specific text ordering and reindex its elements |
| Related functions | Collator::asort() preserves original keys; sort() uses PHP comparison rules. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing key sorting with value sorting | Check whether the comparison applies to keys or values before selecting the sort method. |
| Expecting an unchanged collection | The sorting operation updates the array or collection in place; copy it first when you need the original. |
| Ignoring comparison rules | Requires 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.