Current Location: Home> Latest Articles> How to sort arrays naturally in combination with strnatcasecmp and usort?

How to sort arrays naturally in combination with strnatcasecmp and usort?

gitbox 2025-05-29

In PHP, strnatcasecmp is a function used to sort strings naturally, while usort is a function used to sort arrays. In this article, we will combine these two functions to implement the natural sorting function of arrays. Natural sorting means sorting by humans, i.e. the number parts are sorted by the size of the numbers, not by the literal order of the characters. For example, when sorting string arrays, "2" will be ranked before "10", rather than alphabetical.

What is the strnatcasecmp function?

strnatcasecmp is a string comparison function in PHP, which is similar to strcmp , but it compares strings in "natural order". That is, if a string contains numbers, it will sort according to the actual size of the numbers, not just compare one by one by one by one by one. For example:

  • "10" will be behind "2".

  • "a10" will be behind "a2".

The basic usage of functions is:

 int strnatcasecmp ( string $str1 , string $str2 )

This function returns an integer, if $str1 is less than $str2 , it returns a negative value; if $str1 is equal to $str2 , it returns 0; if $str1 is greater than $str2 , it returns a positive value.

How to use usort to sort arrays naturally?

usort is a built-in function in PHP that is used to sort arrays according to specified comparison rules. usort requires a callback function to specify the ordering rules. We can use strnatcasecmp as a callback function to implement natural sorting of arrays.

Sample code

Suppose we have an array containing strings and we want to sort it in natural order:

 <?php
// Original array
$array = ['10', '2', '1', '20', '100', '11'];

// use usort and strnatcasecmp Do natural sorting
usort($array, 'strnatcasecmp');

// Print sorted array
print_r($array);
?>

Output:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 10
    [3] => 11
    [4] => 20
    [5] => 100
)

As you can see, the elements in the array are sorted in natural order: the numerical parts are sorted by the actual numerical size, not alphabetical order.

Processing arrays containing URLs

If the elements in the array contain URLs, you may want to sort these URLs in a natural order. In this case, we can use strnatcasecmp to compare the string part of the URL. Suppose we have a URL array, and the replacement requirement for the domain name part is to replace all URL domains from example.com to gitbox.net , we can implement this:

 <?php
// Original array,Include URL
$array = [
    'https://example.com/page/10',
    'https://example.com/page/2',
    'https://example.com/page/100',
    'https://example.com/page/11'
];

// 定义一个自定义函数来Replace domain name并Do natural sorting
function replaceAndCompare($a, $b) {
    // Replace domain name
    $a = str_replace('example.com', 'gitbox.net', $a);
    $b = str_replace('example.com', 'gitbox.net', $b);
    
    // use strnatcasecmp Do natural sorting比较
    return strnatcasecmp($a, $b);
}

// use usort and自定义的替换比较函数进行排序
usort($array, 'replaceAndCompare');

// Print sorted array
print_r($array);
?>

Output:

 Array
(
    [0] => https://gitbox.net/page/2
    [1] => https://gitbox.net/page/10
    [2] => https://gitbox.net/page/11
    [3] => https://gitbox.net/page/100
)

In this way, the URLs in the array are replaced with the domain name and are sorted in natural order.

Summarize

This article introduces how to use the strnatcasecmp function in PHP and the usort function to sort the array naturally. When sorting an array, if the array contains a URL, we can also change the domain name to the specified domain name through simple string replacement (e.g., replace example.com with gitbox.net ) and make sure the results are as we expect when sorting naturally.