sort()
Sorts the array (in place).
Syntax
arr.sort((a, b) => a - b)
Description
Default sorting is as TEXT (10, 2 -> 10 comes first!). Provide a comparator for numbers: (a,b)=>a-b.
Example
Loading editor…
Expected output
[ 2, 4, 10, 33 ]
When to use?
Leaderboard, alphabetical/numerical sorting.
Common mistakes
- ⚠️ sort without a comparator sorts numbers like text — resulting in [1,10,2].