Arrays7 min readExamples run on PHP 8.5.10

sort()

Learn how PHP 8.4 sort() orders array values in place, how its flags change comparisons, and which mistakes to avoid.

Signature

sort(array &$array, int $flags = SORT_REGULAR): true

Purpose

sort() arranges the values of an array in ascending order. It changes the array you pass in rather than returning a sorted copy. Use it when you only care about the values and not the keys, for example a list of names, scores or tags that should be displayed in order.

Signature

Parameters and defaults

  • $array is taken by reference, so you must pass a variable. The variable is modified directly.
  • $flags controls how items are compared. The default is SORT_REGULAR, which compares items with PHP's normal comparison rules.

The available flags are:

  • SORT_REGULAR: compare items normally.
  • SORT_NUMERIC: compare items numerically.
  • SORT_STRING: compare items as strings.
  • SORT_LOCALE_STRING: compare items as strings using the current locale. This depends on the environment, so avoid it where you need predictable output.
  • SORT_NATURAL: compare strings with natural ordering, like natsort().
  • SORT_FLAG_CASE: combine it with SORT_STRING or SORT_NATURAL using | to ignore case.

Return value and failure cases

Since PHP 8.2 the declared return type is true; before that it was bool. The function always returns true, so checking the result tells you nothing. The useful outcome is the change to the array. Nothing is signalled for an empty array: it is left as is and true is returned.

The function also resets the array's internal pointer to the first element. Elements that compare as equal keep their original relative order, which is guaranteed from PHP 8.0.

Example 1: basic sorting

The values are put in alphabetical order and the keys are renumbered from zero.

Example
<?php
$fruits = ["lemon", "orange", "banana", "apple"];
$result = sort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);
Output
["apple","banana","lemon","orange"]
bool(true)

Example 2: flags change the comparison

Numeric strings are compared as numbers by default, but SORT_STRING compares them character by character.

Example
<?php
$default = ["10", "9", "2", "1"];
$asString = ["10", "9", "2", "1"];
sort($default);
sort($asString, SORT_STRING);
echo json_encode($default, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($asString, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
["1","2","9","10"]
["1","10","2","9"]

Example 3: natural order without case

Combining SORT_NATURAL with SORT_FLAG_CASE treats digit runs as numbers and ignores letter case. Plain case-insensitive string sorting puts orange20 before Orange3.

Example
<?php
$natural = ["orange20", "Orange3", "orange2", "Orange1"];
$stringOrder = ["orange20", "Orange3", "orange2", "Orange1"];
sort($natural, SORT_NATURAL | SORT_FLAG_CASE);
sort($stringOrder, SORT_STRING | SORT_FLAG_CASE);
echo json_encode($natural, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($stringOrder, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
["Orange1","orange2","Orange3","orange20"]
["Orange1","orange2","orange20","Orange3"]

Edge cases

  • Keys are discarded. String keys and gaps in integer keys are replaced by a fresh list of keys starting at 0. If the association between key and value matters, use asort() or ksort() instead.
  • Mixed types are risky. With SORT_REGULAR, an array containing numbers, numeric strings, non-numeric strings and other types can sort in surprising ways, because the comparison rules differ by type pair. Clean the data first or choose an explicit flag.
  • Uppercase and lowercase. Under SORT_STRING without SORT_FLAG_CASE, comparison is byte-based, so ASCII uppercase letters come before lowercase ones.
  • Equal values keep their order. This matters little for plain scalars but is useful to know when comparing loosely equal values such as "1" and 1.
  • Descending order. sort() has no direction option. Use rsort() for descending order.

Common mistakes

  1. Assigning the result. Writing $sorted = sort($items); stores true, not an array. Call sort($items); and then use $items.
  2. Passing something that is not a variable. The argument is by reference, so a literal or a function call result cannot be sorted in place. Put the data in a variable first.
  3. Expecting keys to survive. After sorting, an old key such as "b" is gone.
  4. Relying on the default flag for text that looks numeric. IDs like "10" and "9" sort numerically by default. Use SORT_STRING if they should be treated as text.
  5. Assuming a particular algorithm. The manual describes a quicksort implementation as a detail you should not depend on. Depend only on the documented ordering and stability.
  • rsort() sorts values in descending order.
  • asort() and arsort() sort by value but keep keys.
  • ksort() and krsort() sort by key.
  • usort() sorts values using your own comparison callback.
  • natsort() and natcasesort() apply natural ordering and keep keys.

Recap

sort() sorts an array's values ascending, in place, and renumbers the keys. It always returns true, so ignore the return value. Pick a flag when the default comparison is not the one you want, and avoid mixed-type arrays with SORT_REGULAR.

Tested examples

Basic ascending sort and return value
<?php
$fruits = ["lemon", "orange", "banana", "apple"];
$result = sort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);
Output
["apple","banana","lemon","orange"]
bool(true)
Default flag versus SORT_STRING on numeric strings
<?php
$default = ["10", "9", "2", "1"];
$asString = ["10", "9", "2", "1"];
sort($default);
sort($asString, SORT_STRING);
echo json_encode($default, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($asString, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
["1","2","9","10"]
["1","10","2","9"]
Case-insensitive natural order versus string order
<?php
$natural = ["orange20", "Orange3", "orange2", "Orange1"];
$stringOrder = ["orange20", "Orange3", "orange2", "Orange1"];
sort($natural, SORT_NATURAL | SORT_FLAG_CASE);
sort($stringOrder, SORT_STRING | SORT_FLAG_CASE);
echo json_encode($natural, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
echo json_encode($stringOrder, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
["Orange1","orange2","Orange3","orange20"]
["Orange1","orange2","orange20","Orange3"]