SpinTheMic
Pro

Interview practice question

Sorting numbers gives the wrong order

Frontend Engineering · JavaScript Core · Beginner

Sorting an array of numbers with arr.sort() gives [1, 10, 2, 25]. Why, and how do you fix it?

What a strong answer covers

Your AI report card counts how many of these ideas you cover.

  • Default sort compares as strings.
  • Pass comparator (a, b) => a - b.
  • Descending b - a.
  • Mutates array.
  • localeCompare for strings.
Practise this topic

Start without signing up. Sign in free to get an AI report card on your answer.

See all topics