01 · THE IDEAREAD → TRY → LEARN
Why sorting needs care
Sorting looks simple until you ask three questions. Does the original array change? What happens to the keys? How are the values compared? PHP's sort() has precise answers to all three, and knowing them stops a lot of subtle bugs.
sort() changes the array you give it
The signature is sort(array &$array, int $flags = SORT_REGULAR): true. The ampersand means the array is passed by reference, so the function rearranges your variable directly instead of returning a new array. The return value is always true, so writing $sorted = sort($data) gives you a boolean, not a sorted array.
$scores = [30, 10, 20];
$result = sort($scores);
echo json_encode($result), "\n";
echo json_encode($scores), "\n";true [10,20,30]
The first line prints the return value and the second prints the rearranged variable. The values are in ascending order, and equal values keep their original relative order.
sort() discards keys
sort() assigns fresh keys 0, 1, 2, ... to the result. Any string keys or gaps in the original numbering are lost. That is ideal for a plain list, but wrong for a map where the key carries meaning, such as a name pointing to an age.
When the association between key and value matters, use a function built to preserve it. asort() sorts by value and keeps each key attached to its value. The next example sorts one copy with sort() and another with asort(), leaving the original untouched.
$ages = ['bo' => 30, 'al' => 20, 'cy' => 25];
$reindexed = $ages;
sort($reindexed);
$keep = $ages;
asort($keep);
echo json_encode($ages), "\n";
echo json_encode($reindexed), "\n";
echo json_encode($keep), "\n";{"bo":30,"al":20,"cy":25}
[20,25,30]
{"al":20,"cy":25,"bo":30}
PHP arrays are copied on assignment in practice, so changing $reindexed never affects $ages. This is the key to producing a sorted copy.
Flags decide how values are compared
The default flag is SORT_REGULAR, which compares values with the normal comparison rules. Mixing strings, numbers and other types under that flag can give surprising orderings, so for mixed data pick an explicit flag. SORT_NUMERIC compares numerically and SORT_STRING compares as strings. The same numeric-looking strings can be ordered two different ways.
$values = ["10", "9", "2", "1"];
$asText = $values;
sort($asText, SORT_STRING);
$asNumbers = $values;
sort($asNumbers, SORT_NUMERIC);
echo json_encode($asText), "\n";
echo json_encode($asNumbers), "\n";["1","10","2","9"] ["1","2","9","10"]
As text, "10" comes before "2" because the character 1 sorts before 2. As numbers, ten is correctly the largest. Choosing the comparison on purpose is what makes a sort predictable.
Building a sorted-copy function
A function that takes an array parameter already works on its own copy. Inside it you can call sort() or rsort() freely and return the result without touching the caller's variable. rsort() is the descending counterpart of sort() and also renumbers keys.
The contract for the exercise:
- Accept a list of integers and a boolean
$descendingflag. - Return a new list in ascending order, or descending when the flag is true.
- Return keys as
0, 1, 2, .... - Handle an empty array and duplicate values.
Common mistakes
- Assigning the result of
sort()to a variable and expecting an array. - Sorting a map with
sort()and losing its keys. - Relying on string comparison for numbers, so
10lands before2. - Forgetting that the flag changes the comparison rules, not just the direction.
Recap
sort() works in place, returns true, renumbers keys and compares by the chosen flag. Copy first when you need the original, use a key-preserving function when keys matter, and pick SORT_NUMERIC when values are numbers that might arrive as strings. Now write the function in the exercise.
Functions · Arrays · Types
- Sort copies of numeric input and distinguish key preservation
- Implement and test a deterministic PHP 8.4 function