Sort ascending with a comparator and join the result.
Acceptance criteria
- 1.#out should show 3-20-100.
DSA Tutorial · Sorting
These 10 tasks go with the Sorting in JavaScript chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “You will almost never write a sorting algorithm at work. You will constantly write comparators — and getting one wrong is a bug that survives review because the output looks nearly right.” .
1.Sort numbersexercise · easy · 10 min
Sort ascending with a comparator and join the result.
Passes when: #out should show 3-20-100.
2.Sort descendingexercise · easy · 10 min · practice plan
Sort largest first and join the result.
Passes when: #out should show 100-20-3.
3.Sort by a propertyexercise · easy · 10 min · practice plan
Sort people by age and show the youngest name.
Passes when: #out should show youngest Bo.
4.Sort stringsexercise · easy · 10 min · practice plan
Sort words alphabetically and join them.
Passes when: #out should show ant-bee-cat.
5.Copy before sortingexercise · medium · 10 min · practice plan
Sort a copy and show that the original is untouched.
Passes when: #out should show original intact.
6.Fix the missing comparatorbug fix · easy · 8 min
Default sort compares numbers as text.
Passes when: #out should show 3-20-100.
7.Fix the reversed comparatorbug fix · easy · 8 min · practice plan
Subtracting the other way sorts descending.
Passes when: #out should show 3-20-100.
8.Fix the comparator returning a booleanbug fix · medium · 8 min · practice plan
A comparator must return a number; a boolean coerces badly.
Passes when: #out should show 3-20-100.
9.Fix sort mutating the originalbug fix · easy · 8 min · practice plan
sort works in place, so the source array is reordered too.
Passes when: #out should show original intact.
10.Fix sorting objects without a keybug fix · easy · 8 min · practice plan
Objects stringify to the same text, so nothing reorders.
Passes when: #out should show youngest Bo.
Between them these tasks cover Comparators, Stability and Sorting objects.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.