Current Location: Home> Latest Articles> How to Naturally Sort Strings in PHP Using strnatcmp(): A Practical Guide

How to Naturally Sort Strings in PHP Using strnatcmp(): A Practical Guide

gitbox 2025-09-16

How to Naturally Sort Strings in PHP Using strnatcmp(): A Practical Guide

During development, we often need to sort a set of strings. Traditional sorting methods usually arrange strings in dictionary order, but sometimes we want the results to follow a "natural sort" rule, where numbers are sorted by actual value rather than character order. PHP offers a very useful function strnatcmp() to help us achieve natural sorting.

What Is Natural Sorting?

Natural sorting, as the name suggests, refers to sorting data in a way that aligns with human intuition. For example, when sorting file names, "file10" should come after "file2" rather than before it alphabetically. Traditional dictionary sorting would place "file10" before "file2" because "1" is less than "2", whereas natural sorting sorts based on numeric values.

Introduction to the strnatcmp() Function

PHP's strnatcmp() function is used to compare two strings using natural order. It works similarly to strcmp(), but strnatcmp() intelligently handles numeric parts in strings, enabling human-like sorting rules.

Function Prototype:

<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str1</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str2</span></span><span> )
</span></span>
  • $str1 and $str2: These are the two strings to be compared.

  • Return Value: Returns a negative integer if $str1 is before $str2, 0 if they are equal, or a positive integer if $str1 is after $str2.

How strnatcmp() Works

Unlike traditional string comparison functions, strnatcmp() parses numeric parts of strings and compares them based on their actual numeric value. It compares each part of the strings step by step until the result is determined.

For example, when comparing "file10" and "file2", strnatcmp() first compares the letter part "file", then compares the numeric part 10 with 2. Since 10 is greater than 2, "file10" is placed after "file2".

Example: Using strnatcmp() for Natural Sorting

We can use strnatcmp() to naturally sort a set of file names. The code is as follows:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Array of strings to sort</span></span><span>
</span><span><span class="hljs-variable">$fileNames</span></span><span> = [</span><span><span class="hljs-string">&#039;file2.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file10.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file1.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file12.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file11.txt&#039;</span></span><span>];
<p></span>// Use usort() with strnatcmp() for natural sorting<br>
usort($fileNames, 'strnatcmp');</p>
<p>// Output the sorted result<br>
foreach ($fileNames as $file) {<br>
echo $file . PHP_EOL;<br>
}<br>
?><br>
</span>

Output:

<span><span>file1.txt
file2.txt
file10.txt
file11.txt
file12.txt
</span></span>

In this example, we first create an array of file names, then use the usort() function with strnatcmp() to naturally sort the array. The sorted array meets our expected order, with numeric parts arranged by size.

Other Related Functions

Besides strnatcmp(), PHP also offers strnatcasecmp(), which works similarly but ignores case differences when comparing.

Example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Natural sorting ignoring case</span></span><span>
</span><span><span class="hljs-variable">$fileNames</span></span><span> = [</span><span><span class="hljs-string">&#039;File2.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file10.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;File1.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file12.txt&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;file11.txt&#039;</span></span><span>];
</span><span><span class="hljs-title function_ invoke__">usort</span></span><span>(</span><span><span class="hljs-variable">$fileNames</span></span><span>, </span><span><span class="hljs-string">&#039;strnatcasecmp&#039;</span></span><span>);
<p></span>// Output the sorted result<br>
foreach ($fileNames as $file) {<br>
echo $file . PHP_EOL;<br>
}<br>
?><br>
</span>

Output:

<span><span>File1.txt
File2.txt
file10.txt
file11.txt
file12.txt
</span></span>

Conclusion

By using strnatcmp() and strnatcasecmp(), we can easily naturally sort strings containing numbers, ensuring that the results match human intuition. These functions are ideal for scenarios like file name sorting and version number comparison that require natural sorting.

I hope this article helps you better understand PHP’s natural sorting functions and their applications.