PHP natsort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, natsort() sorts an array in natural order, the way a person would, and keeps the keys.

A normal sort puts "img10" before "img2" because it compares one character at a time. natsort() reads the numbers inside the text as whole numbers, so img2 correctly comes before img10. It is case sensitive.

Syntax

Call natsort() with the values you need. Start with this line.

PHP
natsort($files);
CallWhat it does
natsort($files)Sorts file names or versions so that numbers are compared as numbers.
natcasesort($files)The case-insensitive version of natsort().
array_values($files)Gives the sorted values new keys 0, 1, 2.

natsort() changes the array in place and returns true. The original keys stay attached to their values. It uses the same comparison as strnatcmp().

Library declarations
PHP
natsort(array &$array): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Numbered File Names

The keys are kept, so print_r() shows the sorted order along with the original positions.

PHP
<?php
$files = ["img12.png", "img10.png", "img2.png", "img1.png"];

natsort($files);

print_r($files);

How It Works

  1. The array has four names that contain numbers.
  2. natsort() compares the numbers inside the names as whole numbers.
  3. The order becomes 1, 2, 10, 12, which is what a person expects.
  4. The keys 3, 2, 1, 0 show that every value kept its old key.

Example 2 — Sort Version Labels

Natural sorting is a good fit for version names, where 1.10 is a newer release than 1.9.

PHP
<?php
$versions = ["v1.10", "v1.9", "v1.2"];

natsort($versions);

echo implode(", ", $versions), PHP_EOL;

How It Works

  1. Each label has a number after the dot.
  2. natsort() compares 2, 9 and 10 as numbers, not as text.
  3. That gives v1. 2, then v1.9, then v1.10.
  4. implode() joins the sorted values into one line. A normal sort() would have put v1.10 first.

Example 3 — Case Matters in natsort()

natsort() is case sensitive. Capital letters sort before lowercase letters.

PHP
<?php
$names = ["File10", "File2", "file1", "File1"];

natsort($names);

echo implode(", ", array_values($names)), PHP_EOL;

How It Works

  1. The array mixes capital "File" and lowercase "file".
  2. Capital letters have lower character codes, so every "File. .." item comes before "file1".
  3. Among the capital names, 1, 2 and 10 are in natural number order.
  4. array_values() resets the keys so only the sorted values are printed.

Frequently Asked Questions

It sorts an array in natural order. Numbers inside the strings are compared as numbers, so item2 comes before item10.
Yes. Each value keeps its original key. Use array_values() if you want the keys to be renumbered.
Yes. Uppercase letters come before lowercase letters. Use natcasesort() to ignore the case.
sort() compares character by character and puts item10 before item2. natsort() compares the numbers as whole numbers and keeps the keys.
No. It sorts the array you pass in and returns true.

Next: next()

next() moves the internal array pointer one step forward and returns the value of the new .

next() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful