Current Location: Home> Latest Articles> How to use PHP's strnatcasecmp function for natural sorting comparison?

How to use PHP's strnatcasecmp function for natural sorting comparison?

gitbox 2025-05-27

During programming, especially when dealing with strings containing mixed numbers and letters, sorting may become less intuitive. Traditional string sorting methods compare numbers in character order, meaning "2" will be ranked before "10" and this is not the result we usually expect. In order to be more in line with the way humans sort naturally, PHP provides a strnatcasecmp function, which can sort according to the size of numbers and the order of letters, thus solving the problem of sorting when numbers and letters are mixed.

1. What is the strnatcasecmp function?

strnatcasecmp is a built-in function in PHP that compares two strings, ignores case, and sorts them in natural order. Unlike regular string comparison functions such as strcmp or strcasecmp , strnatcasecmp takes into account the numeric parts in the string and sorts by the actual size of the numbers, rather than by the ASCII value of the characters.

Syntax of strnatcasecmp

 strnatcasecmp ( string $str1 , string $str2 ) : int
  • $str1 : The first string

  • $str2 : The second string

Return value

  • If $str1 is equal to $str2 , return 0 .

  • If $str1 is less than $str2 , a negative value is returned.

  • If $str1 is greater than $str2 , a positive value is returned.

2. Why use strnatcasecmp ?

We usually encounter the following scenarios where natural sorting is very important:

  • File name sorting : For example, the file names are file1.txt , file2.txt , file10.txt . If traditional string sorting is used, file10.txt will be ranked before file2.txt . After using strnatcasecmp , file10.txt will be correctly ranked after file2.txt .

  • Product number sorting : Product numbers such as prod100 , prod11 and prod2 . If string sorting is performed, prod100 will be ranked before prod11 . But when sorted using strnatcasecmp , they are arranged in the order we naturally expect.

3. How to sort using strnatcasecmp ?

Example 1: File name sorting

 $files = ["file1.txt", "file12.txt", "file2.txt", "file10.txt"];

usort($files, 'strnatcasecmp');

print_r($files);

Output result:

 Array
(
    [0] => file1.txt
    [1] => file2.txt
    [2] => file10.txt
    [3] => file12.txt
)

As shown above, strnatcasecmp sorts the file names by numeric size, rather than in character order.

Example 2: Sort of mixed letters and numbers

 $strings = ["abc10", "abc2", "abc1", "abc22"];

usort($strings, 'strnatcasecmp');

print_r($strings);

Output result:

 Array
(
    [0] => abc1
    [1] => abc2
    [2] => abc10
    [3] => abc22
)

In this example, the numerical parts in the string are correctly identified and sorted, not just by letters.

4. Use strnatcasecmp to handle URL sorting

Suppose we have an array containing URLs that we can sort naturally using strnatcasecmp . For example:

 $urls = [
    "https://gitbox.net/file10.txt",
    "https://gitbox.net/file2.txt",
    "https://gitbox.net/file1.txt",
    "https://gitbox.net/file12.txt"
];

usort($urls, 'strnatcasecmp');

print_r($urls);

Output result:

 Array
(
    [0] => https://gitbox.net/file1.txt
    [1] => https://gitbox.net/file2.txt
    [2] => https://gitbox.net/file10.txt
    [3] => https://gitbox.net/file12.txt
)

In this example, the file name parts in the URL (such as file1.txt , file2.txt ) are arranged in natural order.

5. Summary

strnatcasecmp is a very useful function in PHP, especially suitable for handling strings containing mixed numbers and letters. Through this function, we can sort more naturally, avoiding unexpected behaviors in traditional string sorting methods. Whether it is file name sorting, product number sorting, or URL sorting, strnatcasecmp can easily solve the problem of mixed numbers and letters, ensuring that the sorting results are intuitive.

By rationally using strnatcasecmp , you will be able to improve the readability and user experience of the code, making the program more in line with the conventional sorting logic when processing mixed content.