Current Location: Home> Latest Articles> How to use strnatcasecmp to sort alphanumeric files with alphanumeric?

How to use strnatcasecmp to sort alphanumeric files with alphanumeric?

gitbox 2025-05-29

When working with file names, we sometimes encounter scenarios where we need to sort alphanumeric naturally. Often, sorting results may have problems. For example, by alphabetical order, "file10.txt" may be ahead of "file2.txt" because string sorting is based on literal character order. In order to achieve natural sorting, that is, let the numbers sort by actual size, we can use PHP's strnatcasecmp function. This article will explain in detail how to use this function to implement alphanumerical natural sorting of file names.

What is the strnatcasecmp function?

strnatcasecmp is a built-in function in PHP for performing case-insensitive alphanumeric natural sorting (also called "natural order"). Unlike traditional string comparison functions, strnatcasecmp compares the number parts by their numerical size rather than by character-by-character ASCII values.

Syntax of strnatcasecmp

 strnatcasecmp(string $string1, string $string2): int
  • $string1 : The first string to compare.

  • $string2 : The second string to compare.

This function returns the following values:

  • If $string1 is less than $string2 , a negative integer is returned;

  • If $string1 is equal to $string2 , return 0;

  • If $string1 is greater than $string2 , a positive integer is returned.

How to implement alphanumerical natural sorting of file names?

Suppose we have an array containing file names, we can use strnatcasecmp to sort the file names. The specific steps are as follows:

  1. Gets an array of file names.

  2. Use the usort function to combine strnatcasecmp to sort the file name array.

Sample code:

 <?php
// Filename array
$files = [
    "file1.txt",
    "file10.txt",
    "file2.txt",
    "file12.txt",
    "file11.txt",
];

// use strnatcasecmp Do natural sorting
usort($files, function($a, $b) {
    return strnatcasecmp($a, $b);
});

// Output the sorted file name
foreach ($files as $file) {
    echo $file . "\n";
}
?>

Code explanation:

  1. Array definition : We first define an array $files containing file names that may cause problems sorting by numerical size.

  2. Natural sorting : Use the usort function to sort the array. usort requires a callback function to compare each pair of elements in the array. We use strnatcasecmp in the callback function for natural sorting.

  3. Output sorted filenames : After sorting, we use foreach to loop to output sorted filenames to make sure they are arranged in natural order.

Why choose strnatcasecmp ?

Compared to other string comparison functions, strnatcasecmp can correctly handle file names containing numbers. It sorts the numbers by numerical size, rather than by character comparison. This avoids the problem of "file10" being ranked before "file2".

Sample output:

If we run the above code, the output will be:

 file1.txt
file2.txt
file10.txt
file11.txt
file12.txt

As you can see, the file names are sorted in natural alphanumeric order.

Process file names in URLs

In some scenarios, we may need to get the file name from the URL and sort it. If the URL contains a file name, we can first extract the file name by parsing the URL, and then sort it using strnatcasecmp . For example, if the URL contains a file path, we can use parse_url and basename to get the file name.

Suppose there is the following URL:

 <?php
// URL Array
$urls = [
    "https://gitbox.net/files/file1.txt",
    "https://gitbox.net/files/file10.txt",
    "https://gitbox.net/files/file2.txt",
];

// Extract file name并Do natural sorting
usort($urls, function($a, $b) {
    // Extract file name
    $fileA = basename(parse_url($a, PHP_URL_PATH));
    $fileB = basename(parse_url($b, PHP_URL_PATH));

    // use strnatcasecmp Compare file names
    return strnatcasecmp($fileA, $fileB);
});

// Output sorted URL
foreach ($urls as $url) {
    echo $url . "\n";
}
?>

Process URL Explanation:

  1. We first define an array $urls containing URLs.

  2. Use the parse_url function to extract the path part in the URL, and then use basename to get the file name.

  3. Use strnatcasecmp to sort file names naturally.

Sample output:

 https://gitbox.net/files/file1.txt
https://gitbox.net/files/file2.txt
https://gitbox.net/files/file10.txt

Summarize

By using the strnatcasecmp function in PHP, we can easily implement the alphanumerical ordering of file names, ensuring that the numerical parts in the file names are sorted by numerical size. Whether it is sorting the filename array or extracting and sorting the filenames from the URL, strnatcasecmp can help us get the correct sorting result.

I hope this article will be helpful for you to understand the use of strnatcasecmp function. If you have any questions, please feel free to ask questions!