Signature
sort(array &$array, int $flags = SORT_REGULAR): truePurpose
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
$arrayis taken by reference, so you must pass a variable. The variable is modified directly.$flagscontrols how items are compared. The default isSORT_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, likenatsort().SORT_FLAG_CASE: combine it withSORT_STRINGorSORT_NATURALusing|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.
$fruits = ["lemon", "orange", "banana", "apple"];
$result = sort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);["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.
$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";["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.
$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";["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, useasort()orksort()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_STRINGwithoutSORT_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"and1. - Descending order.
sort()has no direction option. Usersort()for descending order.
Common mistakes
- Assigning the result. Writing
$sorted = sort($items);storestrue, not an array. Callsort($items);and then use$items. - 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.
- Expecting keys to survive. After sorting, an old key such as
"b"is gone. - Relying on the default flag for text that looks numeric. IDs like
"10"and"9"sort numerically by default. UseSORT_STRINGif they should be treated as text. - 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.
Related functions
rsort()sorts values in descending order.asort()andarsort()sort by value but keep keys.ksort()andkrsort()sort by key.usort()sorts values using your own comparison callback.natsort()andnatcasesort()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
$fruits = ["lemon", "orange", "banana", "apple"];
$result = sort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);
["apple","banana","lemon","orange"] bool(true)
$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";
["1","2","9","10"] ["1","10","2","9"]
$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";
["Orange1","orange2","Orange3","orange20"] ["Orange1","orange2","orange20","Orange3"]