To sort an array in PHP, use sort() for values in a list, asort() to sort values and keep keys, ksort() to sort by key, and usort() to sort with your own comparison function. Each has a reverse form (rsort(), arsort(), krsort()). All of them sort the array in place, and since PHP 8.0 the sort is stable, so equal elements keep their original order.
This guide shows how a php sort array call differs by function, how flags change comparison, and how to write a safe usort() callback with the spaceship operator. Examples target PHP 8.3 or later.
sort() and rsort()asort() and ksort()usort()
| Function | Sorts by | Keeps keys | Direction |
|---|---|---|---|
sort() | value | no, renumbers | ascending |
rsort() | value | no, renumbers | descending |
asort() | value | yes | ascending |
arsort() | value | yes | descending |
ksort() | key | yes | ascending |
krsort() | key | yes | descending |
usort() | value, custom | no, renumbers | your rule |
uasort() | value, custom | yes | your rule |
uksort() | key, custom | yes | your rule |
All of them return true and modify the array you pass. They do not return the sorted array, so $sorted = sort($a) is a common mistake.
sort() and rsort()Use these for plain lists where you do not care about the original keys. They throw away the old keys and assign new ones from zero, so do not use them on associative arrays unless that is what you want.
asort() and ksort()asort() and arsort() are what you want for leaderboards and word counts, because the key stays attached to its value.
Every built-in sort accepts a second argument that controls how values are compared.
The common flags are SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_NATURAL and SORT_FLAG_CASE (combined with SORT_STRING or SORT_NATURAL). SORT_LOCALE_STRING uses the current locale. Pick SORT_NATURAL for names with numbers in them.
With the default flag, PHP compares values with the standard comparison rules, which changed in PHP 8.0 for numeric strings against non-numeric ones. When an array mixes numbers and strings, sort results can surprise you, so pass an explicit flag such as SORT_STRING or SORT_NUMERIC.
usort()usort() takes a callback that receives two elements and returns a negative number, zero or a positive number. The spaceship operator <=> does exactly that.
Do not return $a - $b from the callback. It can overflow for large integers, and for floats it gets truncated to an integer, so 0.5 becomes 0 and counts as equal. Always use <=>.
Compare arrays with the spaceship operator to sort by one field and break ties with another.
Swap the two sides for a field you want descending, for example [$a['city'], $b['age']] <=> [$b['city'], $a['age']].
uasort() and uksort()uasort() sorts values with a custom callback and keeps keys. uksort() sorts by key with a callback, useful for ordering keys like month names.
Before PHP 8.0 the sort was not stable, so elements that compared equal could swap places. Since 8.0 they keep their relative order. This means you can sort by one field and then by another, and the first ordering survives among ties.
sort() return true and not the array?Because it sorts the array you pass by reference. Use the same variable afterwards. If you need the original kept, copy it into another variable first.
Use rsort(), arsort() or krsort(). With usort(), swap the operands in the comparison: $b['x'] <=> $a['x'].
Use sort($a, SORT_FLAG_CASE | SORT_STRING), or compare with strcasecmp() inside usort().
Use usort() with a callback that compares the column, or array_multisort() with array_column() when you want a fast, flag-driven sort.
Choose the function by two questions: do you sort by value or key, and do you need to keep keys. Use sort() and rsort() for lists, asort() and ksort() for associative arrays, and usort() with the spaceship operator for custom rules. Add flags such as SORT_NATURAL when the default comparison is wrong, and rely on the stable sort that PHP 8.0 introduced when you chain orderings.