Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Sort Numbers Ascending

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 172: Sorting Arrays

Implement `sortAsc(arr)` returning a new array sorted ascending, without mutating the input.

Sample tests

Input: sortAsc([3,1,2])
Output: [1,2,3]
Input: sortAsc([10,2])
Output: [2,10]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • The default sort is lexicographic, so [10, 2] would become [10, 2] wrong as strings.
  • sort mutates in place.
Visualize this concept: Sorting Arrays →
Approach & explanation (try first)

Copying then sorting with a numeric comparator returns a correctly ordered new array.

Loading...
⌘/Ctrl + Enter

Run your code to see results.