PHP Reference ยท Array Functions
PHP sort flags and sort stability
Learn PHP's sort flags SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_NATURAL and SORT_FLAG_CASE, how they change comparison, and why PHP 8 sorting is stable.
Most PHP sorting functions take a $flags argument that decides how two values are compared. The default, SORT_REGULAR, uses PHP's ordinary comparison rules, which can treat "10" and "9" as numbers but "abc" and "abd" as text. The other flags let you pick one rule for every element so the result does not depend on what the data happens to look like.
Since PHP 8.0 all built-in sorts are also stable: elements that compare as equal stay in their original relative order. This page describes both ideas.
<?php $list = ["10", "9", "2", "1"]; sort($list, SORT_STRING); // "1", "10", "2", "9" sort($list, SORT_NUMERIC); // "1", "2", "9", "10"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
The flags
| Flag | Comparison |
|---|---|
SORT_REGULAR |
Normal PHP comparison (the default). Numeric strings compare as numbers, other strings as text. |
SORT_NUMERIC |
Converts both values to numbers. Non-numeric text counts as 0. |
SORT_STRING |
Compares as strings byte by byte, so "10" sorts before "9" and uppercase before lowercase. |
SORT_LOCALE_STRING |
Compares as strings using the current locale (setlocale()). |
SORT_NATURAL |
Natural order like strnatcmp(): digit runs compare as numbers. |
SORT_FLAG_CASE |
Combine with SORT_STRING or SORT_NATURAL using ` |
Flags work with sort, rsort, asort, arsort, ksort, krsort (array_unique() and array_multisort() accept flags too). The u... functions take a callback instead of flags.
Stability
A sort is stable when two elements that compare as equal keep the order they had before sorting. PHP's sorts have been stable since version 8.0. Before that, the order of equal elements was undefined and could differ between runs and versions.
Stability lets you sort in passes: sort by the least important field first, then by the most important one. Equal items in the final pass keep the order of the earlier pass.
Usage notes
- Pick a flag when the data is uniform. Sorting IDs that arrive as strings? Use
SORT_NUMERIC. Sorting names? UseSORT_STRING | SORT_FLAG_CASE. SORT_REGULARwith mixed types follows the PHP 8 comparison rules (a number compared with a non-numeric string is compared as strings). The order can then be inconsistent, so avoid sorting mixed data without a flag.SORT_FLAG_CASEalone does nothing; it must be combined withSORT_STRINGorSORT_NATURAL.- Locale sorting depends on the locale installed on the server, so results may differ between machines. For accented text, consider
Collatorfrom the intl extension. - Descending order uses the
rfunctions with the same flags.