JavaScript Sorting Gotchas Quiz

JavaScript's sort() has some famous surprises. This quiz tests the default string ordering, compare functions, in-place sorting vs toSorted(), stable sorts and the comparator bugs that pass a quick test but fail later.

Medium 10 questions ~6 minutes Pass at 80% Node.js 22

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 10

What does this print?

const nums = [10, 1, 5, 100];
console.log(nums.sort().join(","));
Show the answer

C. 1,10,100,5 Without a compare function, sort() converts items to strings and compares them character by character. '100' comes before '5' because '1' is less than '5'.

What this quiz covers

  • Why the default sort() compares items as strings
  • Writing compare functions like (a, b) => a - b
  • sort() changes the array, toSorted() returns a copy
  • Case, localeCompare() and undefined items
  • Sorting objects by a property, and stability

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.