Categories
PHP

Comparing Strings with Natural Order Algorithm

Learn how comparison (a string comparison) actually made and how to perform the natural order comparison with stratum() and strnatcasecmp() functions. The natural order comparison treats the string’s numeric parts separately, these functions implement an algorithm that compares alphanumeric strings in the way a human being would.

How comparison actually made

Each character has a numerical ASCII value, you can use the ord() function to get the value for a character. For example, the ASCII values for the uppercase alphabet A-Z are in the range of 65-90, the lowercase alphabet a-z are in the range 97-122, and the numbers 0-9 are in the range 48-57. See the following table:

ASCII values

The ASCII value of a is 97 and ASCII value of A is 65, we see that 97 is greater than the 65. When we compare a and A with strcmp function, it returns < 0 if the first argument is less than the second argument, > 0 if the first argument is greater than the second argument, and 0 if they are equal.

<?php
 echo strcmp('a', 'A');
 //Prints '1'

 echo strcmp('A', 'a');
 //Prints '-1'

 echo strcmp('a', 'a');
 //Prints '0'

If a string contains multiple characters then each character is compared in sequence. For example, comparing ab and AB, the first character a ASCII value is 97 which is greater than the character a ASCII value 65, so the strcmp function would return 1, as the first argument is greater than the second.

<?php
 echo strcmp('ab', 'AB');
 //Prints '1'

If the first character in the first argument is greater (or less) than the first character in the second argument, the other characters comparison is irrelevant. But what if a string contains numbers? In this situation, you must use the strnatcmp and strnatcasecmp functions.

strnatcmp

<?php
//Syntax
strnatcmp(string $string1, string $string2): int

This function returns:

  • < 0 if string1 is less than string2,
  • > 0 if string1 is greater than string2, and
  • 0 if string1 and string2 are equal.

This function compares characters in two strings using the ASCII values, but if there are any numbers within the string they are compared in the natural order. For example, in natural order 19 is greater than 9 so the USD 19 evaluates to greater than USD 9. This function reads the numeric value regardless of its position in the string.

<?php
 echo strcmp('USD 10', 'USD 5');
 //Prints '-1'

 echo strnatcmp('USD 10', 'USD 5');
 //Prints '1'

strnatcasecmp

<?php
//Syntax
strnatcasecmp(string $string1, string $string2): int

This function works similar to the strnatcmp function except it is case-insensitive.

<?php
 echo strnatcmp('USD 10', 'usd 5');
 //Prints '-1'

 echo strnatcasecmp('USD 10', 'usd 5');
 //Prints '1'

Working with Strings: