- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AI / Machine LearningNeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Learn RustOwnership, traits, error handling, and threads Learn GoSlices, interfaces, goroutines, and channels Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- QuizzesNew
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
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.
Sample question
Question 1 of 10What 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()andundefineditems - 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.