Collator::sort
collator_sort(Collator $object, array &$array, int $flags = Collator::SORT_REGULAR): bool
ICU-powered locale sorting: a Collator carrying its own locale rules sorts an array in place (the locale-aware equivalent of sort()), and is the official replacement for strcoll/SORT_LOCALE_STRING deprecated as of PHP 8.6. Under en_US, apple sorts before Ápple — collation keys are maintained release-by-release by ICU and stable across platforms.
C C23
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int by_coll(const void *a, const void *b) {
return strcoll(*(const char *const *)a, *(const char *const *)b);
}
int main(void) {
/* under the "C" locale strcoll() == strcmp(): pure byte order */
setlocale(LC_COLLATE, "C");
const char *words[] = {"banana", "\xc3\x81pple", "apple", "cherry"}; /* Ápple */
qsort(words, 4, sizeof *words, by_coll);
printf("%s,%s,%s,%s\n", words[0], words[1], words[2], words[3]);
return 0;
}
apple,banana,cherry,Ápple
C++ C++26
#include <algorithm>
#include <clocale>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
int main() {
// same as C: under the "C" locale strcoll() == strcmp(), pure byte order
std::setlocale(LC_COLLATE, "C");
std::vector<std::string> words = {"banana", "\xc3\x81pple", "apple", "cherry"}; // Ápple
std::sort(words.begin(), words.end(), [](const std::string &a, const std::string &b) {
return strcoll(a.c_str(), b.c_str()) < 0;
});
std::cout << words[0] << ',' << words[1] << ',' << words[2] << ',' << words[3] << "\n";
return 0;
}
apple,banana,cherry,Ápple
C# v11.0.100
// CompareInfo carries the culture's collation rules
using System.Globalization;
string[] words = { "banana", "Ápple", "apple", "cherry" };
Array.Sort(words, StringComparer.Create(new CultureInfo("en-US"), false));
Console.WriteLine(string.Join(",", words));
apple,Ápple,banana,cherry
Go v1.27.4
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
// stdlib has no ICU collator: sort.Strings is plain byte order
words := []string{"banana", "Ápple", "apple", "cherry"}
sort.Strings(words)
fmt.Println(strings.Join(words, ","))
// localized collation lives in golang.org/x/text/collate (see notes)
}
apple,banana,cherry,Ápple
Java v27.0.1
import java.text.Collator;
import java.util.Arrays;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// java.text.Collator is the JDK-native ICU sibling
String[] words = {"banana", "Ápple", "apple", "cherry"};
Arrays.sort(words, Collator.getInstance(Locale.US));
System.out.println(String.join(",", words));
}
}
apple,Ápple,banana,cherry
JavaScript v26.13.1
// localeCompare delegates to the same ICU collation rules
const words = ['banana', 'Ápple', 'apple', 'cherry'];
words.sort((a, b) => a.localeCompare(b, 'en-US'));
console.log(words.join(','));
apple,Ápple,banana,cherry
PHP v8.6.0
// Collator carries its own locale: sorting no longer depends on global LC_COLLATE
$coll = new Collator('en_US');
$words = ['banana', 'Ápple', 'apple', 'cherry'];
$coll->sort($words); // in-place, like sort()
echo implode(',', $words), "\n";
apple,Ápple,banana,cherry
Python v3.15.2
import unicodedata
words = ['banana', 'Ápple', 'apple', 'cherry']
# stdlib has no ICU collator; hand-roll a primary-strength key: strip accents,
# then case-fold (unaccented sorts before accented, matching en_US tertiary order)
def sort_key(s):
folded = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore').decode()
return (folded.casefold(), s)
print(','.join(sorted(words, key=sort_key)))
# locale.strxfrm needs OS locales; PyICU is the true Collator equivalent (see notes)
apple,Ápple,banana,cherry
Rust v1.99.1
fn main() {
// std has no ICU collator: slice::sort is plain byte (UTF-8 code unit) order
let mut words = vec!["banana", "Ápple", "apple", "cherry"];
words.sort();
println!("{}", words.join(","));
// localized collation needs crates (icu_collator etc., see notes)
}
apple,banana,cherry,Ápple
TypeScript v26.13.1
// localeCompare delegates to the same ICU collation rules
const words: string[] = ['banana', 'Ápple', 'apple', 'cherry'];
words.sort((a: string, b: string) => a.localeCompare(b, 'en-US'));
console.log(words.join(','));
apple,Ápple,banana,cherry
Language differences
PHP: Collator::sort(&$arr) sorts in place by ICU collation rules (the locale-aware equivalent of sort(), keys are re-sorted); siblings: asort() (stable keys), sortWithSortKeys() (faster on large arrays); Collator::compare() is the pairwise comparator and getSortKey() yields a reusable binary sort key. This entry closes the PHP 8.6 teaching loop as the official replacement: per the 8.6 deprecated-features list — strcoll() is deprecated (it relies on the global process state set by setlocale(), which any library can change; it is not binary safe; and some locales ship incorrect collation rules), and the SORT_LOCALE_STRING flag is deprecated with it (it sorts via strcoll); the official recommendation is the intl Collator::compare() / Collator::sort() / Collator::asort() / Collator::sortWithSortKeys(). Strength levels: PRIMARY (base letters only: a = á), SECONDARY (accents), TERTIARY (the default — adds case, hence apple < Ápple), QUATERNARY (punctuation), IDENTICAL (code points); tune via setAttribute(ALTERNATE_HANDLING, NON_IGNORABLE) and setStrength().
Determinism argument: the sample array contains accented words, and the en_US tertiary order "apple,Ápple,banana,cherry" is decided by ICU sort keys — the DUCET relative order of Latin letters has been frozen for years and is byte-identical across ICU versions (this machine's 79.1 and CI alike); since Á's leading UTF-8 byte 0xC3 exceeds every ASCII byte, the C-locale/byte-order languages form the control group "apple,banana,cherry,Ápple".
C: the strcoll(3) concept shares the family but is governed by the global LC_COLLATE state; under the "C" locale it degrades to byte-order strcmp (the example's control group); C++ is the same (the example uses strcoll — note libc++'s std::collate facet has platform pitfalls).
C#: StringComparer.Create(culture, ignoreCase) or CompareInfo.Compare; rules ship with .NET (ICU-backed on Linux).
Java: Collator.getInstance(locale) is the JDK-native counterpart, with the same setStrength(Collator.PRIMARY) levels.
JS/TS: localeCompare(b, locale) / Intl.Collator — the sibling native API; pass {sensitivity:'base'} to approximate PRIMARY.
Python: the stdlib offers only locale.strxfrm (tied to the OS-locale global setting — the deprecated school); the example hand-rolls a pure-stdlib primary-strength key (NFKD accent-stripping + casefold) that matches ICU only because the sample is crafted; the real production answer is PyICU.
Go: golang.org/x/text/collate + collate.New(language.English).
Rust: the icu_collator crate (ICU4X); std has byte order only.
Side-by-side: the ICU family (PHP/JS/Java/C#) ships rules; C/C++/Python-stdlib/Go-stdlib/Rust-stdlib give byte order only — global-locale sorting (strcoll/SORT_LOCALE_STRING) is exactly the anti-pattern the PHP 8.6 deprecation list calls out.