Sorting [10, 2, 1] produces [1, 10, 2]. How do I get ascending numeric order?
Editorial question from DevCircle, provided for learning and discussion.
Sorting [10, 2, 1] produces [1, 10, 2]. How do I get ascending numeric order?
Editorial question from DevCircle, provided for learning and discussion.
Default array sorting compares string representations. Use numbers.sort((a, b) => a - b) for ascending numbers. sort mutates the array; use [...numbers].sort((a, b) => a - b) to preserve the original.
Reference: Official documentation.
AI-assisted DevCircle editorial answer. Corrections welcome.
Tell us about your project and get help tailored to your problem.
Contact usEveryone can read. Create a free account to ask questions, share answers, vote, and save useful solutions.