Current Location: Home> Latest Articles> How to sort multiple parameters using strnatcasecmp?

How to sort multiple parameters using strnatcasecmp?

gitbox 2025-05-19

strnatcasecmp is a very practical string comparison function in PHP. It uses a natural ordering algorithm and is case-insensitive. Natural sorting is not just about sorting strings in dictionary order. It takes into account the size of numbers and can be more in line with human sorting habits. Today we will discuss how to use the strnatcasecmp function to sort multiple parameters naturally.

1. Introduction to strnatcasecmp function

The strnatcasecmp function is used to compare whether two strings (case insensitive) conform to the natural sorting order and returns an integer value:

  • Returns 0 to indicate that the two strings are equal

  • Returns a negative value that means the first string is smaller than the second string

  • Return a positive value that means the first string is larger than the second string

The basic syntax is as follows:

 int strnatcasecmp ( string $string1 , string $string2 )

2. How strnatcasecmp applies to multiple parameter sorting

In practical applications, we often need to sort multiple strings. PHP provides usort function and strnatcasecmp to achieve natural sorting. Here is an example showing how to sort multiple strings using strnatcasecmp .

3. Sample code

 <?php
// Array of strings to be sorted
$array = array(
    "img20.jpg",
    "img3.jpg",
    "img100.jpg",
    "img12.jpg",
    "img1.jpg"
);

// use usort and strnatcasecmp Sort arrays
usort($array, 'strnatcasecmp');

// Output sorted results
foreach ($array as $file) {
    echo $file . "<br>";
}
?>

4. Code parsing

In the above code, an array $array containing multiple file names is first defined. These file names contain numeric parts, and it is through strnatcasecmp that can achieve natural sorting. Next, use the usort function to sort the array with strnatcasecmp , and finally output the sorted file name.

5. How to ignore case when sorting

The feature of strnatcasecmp is that it is not case sensitive, so it will automatically ignore the case problem of letters during the sorting process. For example, if there is img1.jpg and IMG1.jpg in the array, they will be considered equal (in natural sorting, img1.jpg will be ahead of img10.jpg ).

6. Customize the sorting rules

You can sort multiple parameters by custom sorting functions. Here is an example showing how to use strnatcasecmp for natural sorting of arrays of strings of different types:

 <?php
// Define a custom sorting function
function customSort($a, $b) {
    return strnatcasecmp($a, $b);
}

// Arrays to be sorted
$array = array(
    "a20", "a2", "a10", "b1", "b10"
);

// use uasort Sorting
uasort($array, 'customSort');

// Output sorted results
foreach ($array as $value) {
    echo $value . "<br>";
}
?>

In this example, the uasort function and strnatcasecmp are used to sort a naturally in order to an array containing letters and numbers. You can see that the elements in the array are arranged in natural order.

7. Processing of URLs

If you need to sort the string arrays containing URLs naturally, strnatcasecmp works well. Suppose we have a set of URLs with domain names and want to sort naturally according to the path, we can use the following code:

 <?php
// Definition contains URL Array of
$urls = array(
    "https://gitbox.net/file20.jpg",
    "https://gitbox.net/file3.jpg",
    "https://gitbox.net/file100.jpg",
    "https://gitbox.net/file12.jpg",
    "https://gitbox.net/file1.jpg"
);

// use usort and strnatcasecmp Sorting
usort($urls, 'strnatcasecmp');

// Output sorted results
foreach ($urls as $url) {
    echo $url . "<br>";
}
?>

8. Summary

With the above examples we can see that the strnatcasecmp function is a very suitable tool for natural sorting, especially when you need to sort strings containing numbers. With sorting functions such as usort or uasort , it can help us easily sort multiple parameters in natural order.

Hopefully this article helps you better understand how to use the strnatcasecmp function to sort multiple strings naturally and apply them to actual development.