Stop 21 · Data Structures & AlgorithmsTopics · 16/17

Sorting

Part of the Frontend Developer Roadmap

In short

You'll mostly call .sort() in real code, but interviewers still ask how sorting works, which sorts are stable, and why JavaScript sorts [10, 9, 1] so strangely.

Algorithms for ordering data (bubble, merge, quick, etc.) — foundational CS knowledge, and the basis for many other algorithms' efficiency assumptions.

The jaadu and a teacher friend merge two neat piles of answer sheets into one.

Think of it like this

Arranging answer sheets by roll number

Two teachers each sort half the pile. Then they sit together and keep picking the smaller roll number from the top of either pile. Sorted halves, merged in one pass. That's merge sort.

Watch it happen

Merge sort on [5, 2, 4, 6, 1, 3]

5
0
2
1
4
2
6
3
1
4
3
5

1/5Split in half: [5, 2, 4] and [6, 1, 3]. Keep splitting until each piece has one item.

The big ideas

What you need to know

O(n log n) is the target

Merge sort, heap sort and the built-in sort are O(n log n). Bubble, selection and insertion are O(n²).

Quick sort's bad day

Quick sort averages O(n log n), but bad pivots can make it O(n²).

Stable sorts keep ties in order

Equal items stay in their original order. JS Array.sort has been required to be stable since ES2019.

JS sorts as strings by default

Without a compare function, [10, 9, 1].sort() compares text, giving [1, 10, 9].

See it in code

Walk through the code

js
1[10, 9, 1].sort();                // [1, 10, 9]  (string order!)2[10, 9, 1].sort((a, b) => a - b); // [1, 9, 10]3 4function mergeSort(arr) {5  if (arr.length <= 1) return arr;6  const mid = Math.floor(arr.length / 2);7  const left = mergeSort(arr.slice(0, mid));8  const right = mergeSort(arr.slice(mid));9 10  const out = [];11  let i = 0, j = 0;12  while (i < left.length && j < right.length) {13    out.push(left[i] <= right[j] ? left[i++] : right[j++]);14  }15  return [...out, ...left.slice(i), ...right.slice(j)];16}

1/4Without a compare function, numbers are sorted as text.

There are log n levels of splitting and each level merges n items, so time is O(n log n). The extra arrays make space O(n). Using <= keeps it stable.

Save yourself the pain

Common mistakes

Oops

Sorting numbers with plain .sort().

Do this instead

Always pass (a, b) => a - b for numbers. For text in Hindi or other languages, use localeCompare.

Oops

Forgetting that .sort() changes the original array.

Do this instead

Copy first with [...arr].sort(), or use arr.toSorted() where your target browsers support it.

Your mission

Sort it yourself

~40 min

Quick check

What does [10, 9, 1].sort() return in JavaScript?

Go deeper

  1. 1Implement merge sort and quicksort from scratch, not just call a built-in.
  2. 2Learn the time/space complexity trade-offs between the common sorting algorithms.
  3. 3Understand why most languages' built-in sort is a hybrid algorithm, not a single 'pure' one.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge