Menu

PHP Sort Array: sort(), usort(), asort() and ksort()

sort($array) sorts a list in place, asort and ksort sort an associative array by value or by key, and usort sorts with your own comparison. Learn all of them, sort a multidimensional array by a column, and avoid the classic $sorted = sort($arr) mistake.

This page includes runnable editors - edit, run, and see output instantly.

sort($array) sorts an array in ascending order, in place. It changes the variable you pass and returns true, so you call it on its own line and then use the array. rsort() sorts in descending order.

PHP has a sort function for each combination of "by value or by key", "ascending or descending" and "keep the keys or not":

FunctionSorts byOrderKeys
sortvalueascendingrenumbered 0, 1, 2
rsortvaluedescendingrenumbered
asortvalueascendingkept
arsortvaluedescendingkept
ksortkeyascendingkept
krsortkeydescendingkept
usortyour functionyour choicerenumbered
uasortyour functionyour choicekept
uksortyour function on keysyour choicekept

Sort an associative array by value

sort() on an associative array throws the keys away. Use asort() to sort by value and keep each key with its value, or arsort() for highest first.

Sort an array by key with ksort

ksort() orders by key, krsort() by key in reverse. This is handy before printing settings or building a query string in a stable order.

Custom order with usort and the spaceship operator

usort($array, $callback) lets you decide the order. The callback gets two elements and returns a negative number if the first should come first, a positive number if the second should, and 0 if they are equal. The spaceship operator <=> returns exactly that.

banana and cherry have the same length, so the callback returns 0 for them and they stay in the order they already had. PHP's sort has been stable since PHP 8.0: equal elements keep their previous order.

Sort a multidimensional array by a value

To sort a list of rows by one field, compare that field in usort. To sort by two fields, compare arrays: <=> compares them element by element, so the second field only breaks ties in the first.

The second comparison puts $b["age"] on the left side and $a["age"] on the right, which reverses only the age order. array_multisort(array_column($people, 'city'), SORT_ASC, array_column($people, 'age'), SORT_DESC, $people) gives the same order without a callback: it sorts by the extracted columns and reorders $people to match.

Natural order and sort flags

By default PHP compares numbers as numbers and strings as strings. File names with numbers in them sort in a way people do not expect: img10 comes before img2, and every uppercase name comes before every lowercase one. The flags change how values are compared.

SORT_NATURAL reads digit runs as numbers, SORT_FLAG_CASE ignores upper and lower case, and SORT_STRING forces a character by character comparison even for numeric strings. natsort() and natcasesort() are the same idea but keep the keys.

Common mistake: assigning the result of sort

Every sort function returns true and changes the array you pass in. Assigning the return value loses the array:

If you need a sorted copy and want to keep the original, copy the array into a new variable first and sort the copy, as the asort example above does with $copy.

Frequently Asked Questions

Why does sort() return true instead of the sorted array?

PHP's sort functions sort the array in place, through a reference, and return true. Call sort($arr); on its own line and use $arr afterwards. Writing $arr = sort($arr); replaces your array with true.

How do I sort an associative array by value and keep the keys?

Use asort($arr) for ascending or arsort($arr) for descending. sort() and rsort() also sort by value but throw the keys away and renumber from 0.

How do I sort a multidimensional array by a value in PHP?

Use usort with the spaceship operator on the field: usort($rows, fn($a, $b) => $a['age'] <=> $b['age']);. Swap $a and $b for descending order, and compare arrays such as [$a['city'], $a['age']] <=> [$b['city'], $b['age']] to sort by two fields.

Is PHP sort stable?

Yes, since PHP 8.0 every sort function is stable: elements that compare as equal keep the order they had before sorting. That is what makes sorting by one field and then another work as expected.

How do I sort strings with numbers in natural order?

Use natsort($arr) or sort($arr, SORT_NATURAL), which put img2 before img10. Add SORT_FLAG_CASE (SORT_NATURAL | SORT_FLAG_CASE) or use natcasesort to ignore upper and lower case.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED