Current Location: Home> Latest Articles> How to use strnatcasecmp for forward and reverse sorting of version numbers in PHP?

How to use strnatcasecmp for forward and reverse sorting of version numbers in PHP?

gitbox 2025-05-27

In PHP, strnatcasecmp() is a very useful string comparison function, especially for string comparisons with numbers, such as version numbers. Unlike the common strcmp() or strcasecmp() , strnatcasecmp() uses a natural sorting method for comparison, that is, it takes into account the numeric size, not just dictionary order. This is very helpful when comparing version numbers, especially version numbers like "1.2.10" and "1.2.2".

This article will introduce how to use the strnatcasecmp() function to achieve forward and reverse sorting of version numbers. We will show how to sort a version number array and implement forward and reverse sorting respectively.

What is the strnatcasecmp() function?

The strnatcasecmp() function is used to compare two strings, ignore case, and use natural sorting rules. This means it takes into account the actual size of the numbers in the string, rather than simply comparing them alphabetically. This function is especially suitable for comparing version number strings, because version number contains numbers, using strcmp() or strcasecmp() directly may cause the sorting to not be as expected.

Function signature:

 int strnatcasecmp ( string $str1 , string $str2 )
  • $str1 and $str2 : Two strings to be compared.

  • Return value: The return value is an integer. If $str1 is less than $str2 , a negative number is returned; if $str1 is greater than $str2 , a positive number is returned; if both are equal, a 0 is returned.

How to implement forward sorting of version numbers?

Forward sorting means sorting from small to large version number. We can implement forward sorting of version numbers through the usort() function combined with strnatcasecmp() .

Sample code:

 <?php
// Version number array
$versions = [
    "1.10.2",
    "1.2.10",
    "1.2.1",
    "2.0.0",
    "1.9.9",
];

// use strnatcasecmp Forward sorting
usort($versions, function($a, $b) {
    return strnatcasecmp($a, $b);
});

// Output the sorted version number
echo "Forward sorted version number:\n";
foreach ($versions as $version) {
    echo $version . "\n";
}
?>

In this code, we define a version number array and sort it using the usort() function. The callback function of usort() uses strnatcasecmp() to compare two version strings, ensuring that the sorting is in numerical size rather than alphabetical order.

Output result:

 Forward sorted version number:
1.2.1
1.2.10
1.9.9
1.10.2
2.0.0

How to implement reverse sorting of version numbers?

Reverse sorting means sorting from large to small version number. To achieve this, you just need to reverse the return value of strnatcasecmp() when sorting.

Sample code:

 <?php
// Version number array
$versions = [
    "1.10.2",
    "1.2.10",
    "1.2.1",
    "2.0.0",
    "1.9.9",
];

// use strnatcasecmp Reverse sorting
usort($versions, function($a, $b) {
    return strnatcasecmp($b, $a); // Reverse the parameter order
});

// Output the sorted version number
echo "Reverse sorted version number:\n";
foreach ($versions as $version) {
    echo $version . "\n";
}
?>

In the reverse sorting code, we achieve ordering from large to small by exchanging the order of parameters in the strnatcasecmp() function. In this way, the maximum value in the version number array will be ranked first.

Output result:

 Reverse sorted version number:
2.0.0
1.10.2
1.9.9
1.2.10
1.2.1

Summarize

Using the strnatcasecmp() function, we can very conveniently sort strings containing numbers, such as version numbers, naturally. By combining the usort() function, both forward sorting of version numbers can be achieved and reverse sorting can be achieved. Natural sorting ensures comparison of numeric sizes rather than simple alphabetical order, thus avoiding problems like "1.10.2" and "1.2.10" sorting errors.

The above is the method of using the strnatcasecmp() function in PHP to implement forward and reverse sorting of version numbers.