Skip to content

Novis\Intl\Collator::Strength

Which differences between two strings change their order when Collator sorts them.

Collator::Strength is declared in the class Novis\Intl\Collator.

Collator sorts strings by the rules of a language. You pass a Collator::Strength to Collator::sort or Collator::sortKeys as strength. Each level compares more than the one before it:

  • Primary compares the letters only. "resume", "Résumé" and "RESUME" are equal.
  • Secondary also compares accents, so "resume" and "résumé" are different.
  • Tertiary also compares upper and lower case. This is the default.
  • Quaternary also compares punctuation when the option ignorePunctuation is true. In Japanese, it also compares Hiragana and Katakana, the two Japanese syllable alphabets.
  • Identical also compares the characters themselves when everything else is equal. It is the slowest level.

ignorePunctuation is another option of Collator::sort and Collator::sortKeys. When it is true, Primary, Secondary and Tertiary do not compare punctuation.

Strings that are equal at the chosen strength keep the order they had in the list.

Good to know: use Primary for a search that should ignore case and accents.

Examples

Compare letters accents and case

<?nvs
// Shows how each `Collator::Strength` changes which names count as equal when they are sorted.
use Novis\Intl\Collator;
array<string> $names = ['resume', 'Résumé', 'résumé', 'Resume'];
// `Primary` compares letters only, so all four names are equal and keep their order.
echo Core\Str::join(Collator::sort($names, 'en', { strength: Collator::Strength::Primary }), ', '), "\n";
// `Secondary` also compares accents, so the names without accents come first.
echo Core\Str::join(Collator::sort($names, 'en', { strength: Collator::Strength::Secondary }), ', '), "\n";
// `Tertiary` also compares case. This is the default.
echo Core\Str::join(Collator::sort($names, 'en', { strength: Collator::Strength::Tertiary }), ', '), "\n";
Output
resume, Résumé, résumé, Resume
resume, Resume, Résumé, résumé
resume, Resume, résumé, Résumé