sort() defaults to string order, so [10, 2, 1].sort() gives [1, 10, 2]. Pass a comparator: sort((a, b) => a - b) for ascending numbers. Both sort and reverse mutate in place — copy first ([...arr].sort()) if you need the original.
sort() defaults to string order, so [10, 2, 1].sort() gives [1, 10, 2]. Pass a comparator: sort((a, b) => a - b) for ascending numbers. Both sort and reverse mutate in place — copy first ([...arr].sort()) if you need the original.
<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const nums=[10,2,1,30]; const sorted=[...nums].sort((a,b)=>a-b);
document.getElementById('o').textContent='default='+[...nums].sort()+'\nnumeric='+sorted;</script></body></html>▶ Open the interactive comic issue