Current Location: Home> Latest Articles> strnatcasecmp vs strcoll: Which is more suitable for natural sorting of strings?

strnatcasecmp vs strcoll: Which is more suitable for natural sorting of strings?

gitbox 2025-05-19

In PHP, we often need to sort or compare strings. In the natural sorting scenario, strnatcasecmp and strcoll are two commonly used string comparison functions. Although both are used to compare strings, they are somewhat different in how they are processed. Today we will make a detailed comparison of these two functions to help you choose the right function to handle the needs of natural string sorting.

1. Function introduction

  • strnatcasecmp :

    • strnatcasecmp is a function in PHP that compares two strings in a "natural sort" way. The so-called "natural sorting" means that the numbers in a string will be treated as numbers, not just characters. Its comparison method ignores case, so in letter comparison, case does not affect sorting.

    • grammar:

       int strnatcasecmp ( string $str1 , string $str2 )
      
    • Return value:

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

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

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

    • Example:

       $str1 = "file10";
      $str2 = "file2";
      echo strnatcasecmp($str1, $str2);  // Output negative number,because "file10" Ranking in natural order "file2" Front
      
  • strcoll :

    • strcoll compares two strings based on the current locale setting. It uses localized rules for string comparisons, so the results may vary depending on different locales. It is suitable for sorting scenarios that need to consider language and cultural differences.

    • grammar:

       int strcoll ( string $str1 , string $str2 )
      
    • Return value:

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

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

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

    • Example:

       setlocale(LC_COLLATE, 'en_US.UTF-8');  // Setting up the area
      $str1 = "apple";
      $str2 = "banana";
      echo strcoll($str1, $str2);  // Output negative number,because "apple" Alphabetical order "banana" Front
      

2. Comparison between strnatcasecmp and strcoll

  • sort by :

    • strnatcasecmp uses the "natural sorting" method, and numbers will be treated as numbers for sorting, ignoring upper and lower case.

    • strcoll compares strings based on the system's locale settings, and the sorting rules will depend on different languages ​​and cultural habits.

  • Use scenarios :

    • If you want to do natural sorting, especially when comparing strings containing numbers, strnatcasecmp is more appropriate. For example, when you want to compare file names, strings like "file1" and "file10" should be sorted in natural order, then strnatcasecmp is a better choice.

    • If you need to compare strings by locale, especially when dealing with multilingual content, strcoll is a more suitable function. For example, strcoll is more flexible when you want strings to be sorted by locale (such as English or French).

  • Performance differences :

    • strnatcasecmp may be slightly slower to process when doing natural sorting, because it requires parsing numbers in the string.

    • strcoll sorts according to the current locale, so performance may be affected by locale, but usually it is more efficient.

3. Code example: Natural sort vs Locale sort

 // use strnatcasecmp Do natural sorting
$file1 = "file10";
$file2 = "file2";
echo strnatcasecmp($file1, $file2);  // Output negative number,file10 exist file2 Before

// use strcoll Sorting locale settings
setlocale(LC_COLLATE, 'en_US.UTF-8');
echo strcoll("apple", "banana");  // Output negative number,because apple 排exist banana Front

4. Conclusion

When sorting strings, strnatcasecmp is a more suitable choice if you need to compare strings in "natural order", especially if the string contains numbers. It can better understand the needs of numeric sorting. On the other hand, strcoll is more suitable for scenarios where you need to sort by language locale, especially when you need to sort multiple languages.

Therefore, which function to choose depends on your actual needs. strcoll might be better if you want to do cross-language string sorting; strnatcasecmp is more applicable if you only care about natural sorting, especially when numbers involve sorting.