Engrain by ByteRush

DSA.04.03 JuniorCore — asked often

Sorting in JavaScript

Comes up in: DSA, JavaScript, Recruiter screen

This concept is being written. It gets the full five steps — explanation, predict, interview angle, practice with tests, and review cards. Here is exactly what it will cover, so you can study it elsewhere in the meantime.

What it covers

  • Array.prototype.sort is stable (required since ES2019)
  • V8 uses TimSort (since V8 7.0)
  • default sort compares strings
  • comparator contract (negative/zero/positive, consistent)
  • sorting objects by multiple keys
  • toSorted
  • localeCompare/Intl.Collator

Words you will know

TimSortcomparator contract

What you will practise

  • multi-key sort
  • fix 4 comparator bugs

Interview questions this answers

  • What does it mean for a sort to be stable? Is JavaScript's sort stable?