Skip to content

Core\Str::compare

Core\Str::compare(
    string $a,
    string $b,
    {caseInsensitive?: bool, natural?: bool},
): int

Compares two strings and tells you which one comes first when you sort them.

Core\Str::compare returns -1 when the first string comes first, 0 when the two are equal, and 1 when the second string comes first. You can use it as the comparison function of a sort.

By default, the comparison is character by character and case-sensitive. All upper case letters come before all lower case letters, so "Zebra" comes before "apple". Two options change this. With { caseInsensitive: true }, upper and lower case letters are equal. With { natural: true }, digits inside the strings are compared as numbers, so "file2" comes before "file10". You can use both options together.

The examples below compare two words, sort file names with numbers in them, and sort a list of customer names without caring about upper and lower case.

Parameters

  • $astringThe first string.
  • $bstringThe second string.

Options

  • caseInsensitivebooloptionalWhen true, upper and lower case letters are equal. ß and SS are still different. The default is false.
  • naturalbooloptionalWhen true, digits inside the strings are compared as numbers, so "img2" comes before "img12". The default is false.

Return value

Returns int. -1 when $a comes first, 0 when the two are equal, and 1 when $b comes first.

Examples

Which comes first

<?nvs
// Shows how to find out which of two strings comes first in sorted order.
// "apple" comes before "banana". This prints `-1`.
echo Core\Str::compare('apple', 'banana'), "\n";
// Two equal strings give `0`. This prints `0`.
echo Core\Str::compare('apple', 'apple'), "\n";
// Upper case letters come before lower case letters. This prints `-1`.
echo Core\Str::compare('Zebra', 'apple'), "\n";
// With `{ caseInsensitive: true }`, "Zebra" is compared as "zebra". This prints `1`.
echo Core\Str::compare('Zebra', 'apple', { caseInsensitive: true }), "\n";
Output
-1
0
-1
1

Numbers inside names

<?nvs
// Shows how to sort file names that contain numbers in the order a person expects.
array<string> $files = ['file10.txt', 'file2.txt', 'file1.txt'];
// By default, the characters are compared one by one. "1" comes before "2",
// so this prints `file1.txt, file10.txt, file2.txt`.
array<string> $plain = Core\Arr::sort(
$files,
{ comparator: fn(string $a, string $b): int => Core\Str::compare($a, $b) },
);
echo Core\Str::join($plain, ', '), "\n";
// With `{ natural: true }`, the digits are compared as numbers.
// This prints `file1.txt, file2.txt, file10.txt`.
array<string> $natural = Core\Arr::sort(
$files,
{ comparator: fn(string $a, string $b): int => Core\Str::compare($a, $b, { natural: true }) },
);
echo Core\Str::join($natural, ', '), "\n";
Output
file1.txt, file10.txt, file2.txt
file1.txt, file2.txt, file10.txt

Sort customer names

<?nvs
// Shows how to sort a list of customer names for a report, without caring about upper and lower case.
array<string> $customers = ['bob', 'Alice', 'carol', 'Dave', 'alice'];
// `{ caseInsensitive: true }` makes "Alice" and "alice" equal, and "bob" comes before "Dave".
// This prints the names in alphabetical order, one per line.
array<string> $sorted = Core\Arr::sort(
$customers,
{ comparator: fn(string $a, string $b): int => Core\Str::compare($a, $b, { caseInsensitive: true }) },
);
foreach ($sorted as string $name) {
echo $name, "\n";
}
Output
Alice
alice
bob
carol
Dave