Novis\Intl\Collator::sortKeys
Novis\Intl\Collator::sortKeys(
array<string> $strings,
string $locale,
?CollateOptions $options = null,
): array<bytes>Gives each string a sort key: bytes that compare in the order of a language and region.
Collator::sortKeys returns one key for each string, in the same order as the list. Compare two keys byte by byte, for example with Core\Bytes::compare. The result is the same order that Collator::sort gives the two strings for the same locale and options. Equal strings have equal keys.
You can store a key in a database column and sort rows by that column. The database then sorts in the order of the language, and it does not need to know the language rules.
The options are the same as for Collator::sort. With strength: Collator::Strength::Primary, "Apple" and "apple" have the same key.
Collator::sortKeys throws a LogicError when the locale tag is not valid.
Good to know: a key belongs to one locale, one set of options and one version of Novis. Compare only keys that were made the same way, and make them again after an upgrade.
Parameters
$stringsarray<string>$localestring$options?CollateOptionsdefault null
Return value
Returns array<bytes>.
Examples
Compare two keys
<?nvs// Shows that two sort keys compare in the order of the language.use Novis\Intl\Collator;
array<bytes> $keys = Collator::sortKeys(['Öl', 'Ofen'], 'de');
// In German, "Öl" sorts after "Ofen", so the first key is greater.// This prints Ofen comes first.if (Core\Bytes::compare($keys[0], $keys[1]) > 0) { echo "Ofen comes first\n";} else { echo "Öl comes first\n";}Ofen comes firstCase does not count
<?nvs// Shows how to make keys that are the same for upper and lower case.use Novis\Intl\Collator;
array<string> $tags = ['Apple', 'apple'];
// With the default strength, case counts. This prints different.array<bytes> $keys = Collator::sortKeys($tags, 'en');echo Core\Bytes::compare($keys[0], $keys[1]) == 0 ? 'same' : 'different', "\n";
// With `Collator::Strength::Primary`, case does not count. This prints same.array<bytes> $loose = Collator::sortKeys($tags, 'en', { strength: Collator::Strength::Primary });echo Core\Bytes::compare($loose[0], $loose[1]) == 0 ? 'same' : 'different', "\n";differentsameFind the first row
<?nvs// Shows how to find the row that sorts first by using its key.use Novis\Intl\Collator;
array<string> $products = ['Zucker', 'Äpfel', 'Mehl', 'Butter'];array<bytes> $keys = Collator::sortKeys($products, 'de');
// This loop keeps the position of the smallest key.int $first = 0;for (int $i = 1; $i < 4; $i++) { if (Core\Bytes::compare($keys[$i], $keys[$first]) < 0) { $first = $i; }}
// This prints Äpfel.echo $products[$first], "\n";Äpfel