W3docs

natcasesort()

Welcome to our comprehensive guide on natcasesort() function in PHP. In this article, we will dive deep into the PHP natcasesort() function and discuss how it

Introduction

natcasesort() sorts an array using a natural order algorithm with a case-insensitive comparison. "Natural order" means it orders strings the way a human would — so img10 comes after img2, not before it like a plain alphabetical sort would do.

This page covers what natural ordering is, how natcasesort() differs from natsort() and sort(), and the gotchas around keys and return values.

Syntax

natcasesort(array &$array): bool

It takes the array by reference, sorts it in place, and returns true on success. You do not assign the result back — the array you pass in is modified directly.

DetailBehavior
SortsIn place (by reference)
KeysPreserved — key/value pairs stay linked
OrderAscending, natural, case-insensitive
Returnsbool (true on success)

Why natural order matters

A regular alphabetical sort compares strings character by character, so "img10" sorts before "img2" because 1 is less than 2. That is rarely what a person expects when filenames or labels contain numbers. Natural order treats the digit runs as numbers, giving the human-friendly result.

Basic example

php— editable, runs on the server

Output:

Array
(
    [3] => Boston
    [2] => Chicago
    [1] => Los Angeles
    [0] => New York
    [4] => San Francisco
)

Notice the keys (3, 2, 1, 0, 4) are not renumbered — each value keeps its original key. If you need keys reset to 0, 1, 2…, use sort() instead, or run the result through array_values().

natcasesort() vs natsort() vs sort()

The difference between these three is easiest to see with mixed-case strings that contain numbers:

<?php

$files = array("IMG10.png", "img12.png", "img2.png", "IMG1.png", "img10.png");

$a = $files;
natcasesort($a);   // natural + case-insensitive
print_r($a);

Output:

Array
(
    [3] => IMG1.png
    [2] => img2.png
    [0] => IMG10.png
    [4] => img10.png
    [1] => img12.png
)

With natsort() (case-sensitive) the same data sorts differently, because uppercase letters compare lower than lowercase ones — so IMG10 lands before img2:

Array
(
    [3] => IMG1.png
    [0] => IMG10.png
    [2] => img2.png
    [4] => img10.png
    [1] => img12.png
)

And sort() (regular, not natural) reindexes the keys and puts img10/img12 before img2:

Array
(
    [0] => IMG1.png
    [1] => IMG10.png
    [2] => img10.png
    [3] => img12.png
    [4] => img2.png
)

Use natcasesort() when the order should match human intuition and case should be ignored — for example listing user-uploaded filenames where someone may have typed IMG or img.

Gotchas

  • It returns a bool, not the sorted array. $sorted = natcasesort($arr); sets $sorted to true, not your data. The data is in $arr.
  • Keys are preserved. This breaks code that assumes 0-based keys afterwards. Wrap with array_values($arr) if you need a clean list.
  • Non-string values are compared as strings. Numbers, booleans, etc. are cast to strings before comparison.
  • natsort() — natural order, case-sensitive.
  • sort() — regular ascending sort, reindexes keys.
  • asort() — ascending sort that preserves keys (not natural).
  • usort() — sort with your own comparison callback.

Available since PHP 4.0.0.

Practice

Practice
What is the function of the 'natcasesort()' function in PHP?
What is the function of the 'natcasesort()' function in PHP?
Was this page helpful?