Current Location: Home> Latest Articles> The difference between strnatcasecmp and natsort in PHP and usage scenarios

The difference between strnatcasecmp and natsort in PHP and usage scenarios

gitbox 2025-05-27

In PHP, strnatcasecmp and natsort both involve the concept of natural sorting, which are usually used to sort strings, but are functionally different. This article will explore in-depth the differences between them and applicable scenarios.

What is natural sorting?

Natural sorting is a sorting method that matches human intuition, that is, numbers are sorted by their values, rather than in the ASCII encoding order of their characters. For example, in natural sorting, 10 will be behind 2 , instead of 10 will be ahead of 2 , like dictionary sorting.

strnatcasecmp function

The strnatcasecmp function is used to compare two strings, uses natural sorting rules, and ignores case. Unlike the strcmp function, strnatcasecmp ignores the case of letters when comparing and sorts them according to human intuition. The signature of this function is as follows:

 int strnatcasecmp ( string $str1 , string $str2 )

Example of usage:

 <?php
$str1 = "file10";
$str2 = "file2";

$result = strnatcasecmp($str1, $str2);
if ($result < 0) {
    echo "$str1 is less than $str2\n";
} elseif ($result > 0) {
    echo "$str1 is greater than $str2\n";
} else {
    echo "$str1 is equal to $str2\n";
}
?>

explain:

  • In the above code, file10 will be considered larger than file2 , because natural sorting will treat the number 10 as greater than 2 .

  • strnatcasecmp ignores the case of letters, so FILE10 and file10 are equivalent.

Applicable scenarios:

strnatcasecmp is suitable for when two strings need to be compared naturally, and the case of letters is not considered. For example, this function can be used when comparing two file names or version numbers.

natsort function

Compared to strnatcasecmp , natsort is a function for natural sorting of arrays. Instead of returning the comparison result of two strings, it directly modifies the array itself and sorts the elements in the array according to the rules of natural sorting. The signature of natsort is as follows:

 bool natsort ( array &$array )

Example of usage:

 <?php
$files = ["file20", "file1", "file10", "file2"];

natsort($files);

print_r($files);
?>

explain:

  • In this example, natsort will sort the $files array naturally, and the sorting result will be:

     Array
    (
        [1] => file1
        [3] => file2
        [2] => file10
        [0] => file20
    )
    
  • file10 will be ranked behind file2 , and file20 will be ranked behind file2 , which complies with the rules of natural sorting.

Applicable scenarios:

natsort is suitable for scenarios where an array needs to be sorted naturally, such as sorting file names, version numbers lists, or any array of strings containing numbers and letters.

The difference between strnatcasecmp and natsort

  • Return value :

    • strnatcasecmp is used to compare two strings and return the comparison result (less than 0, equal to 0, greater than 0).

    • natsort is used to sort an array, returning a boolean value indicating whether it is sorted successfully.

  • Applicable objects :

    • strnatcasecmp is a function that compares two strings.

    • natsort is a sorting function for arrays.

  • sort by :

    • strnatcasecmp does not change the original string, but compares the two strings.

    • natsort will directly modify the order of the array.

Summarize

  • Use the strnatcasecmp function to perform natural sorting comparisons on two strings and ignore case. It is often used to compare individual strings and is suitable for scenarios where comparisons are required.

  • Use the natsort function to sort an array containing strings naturally. It does not return the sorting result, but directly modify the array order. Suitable for arrays that need to be sorted.

These two functions have different application scenarios, and developers can choose appropriate functions according to actual needs.


Hope this article helps you! If you have any questions or need further discussion, please feel free to ask questions.