WorkspaceLearning path
Level 1 · Foundations
BUILD YOUR INTUITION

Sorting

Reorder values by a chosen key so local comparisons reveal global structure, such as neighboring overlapping ranges.

THE MENTAL MODEL

When should this come to mind?

Reorder values by a chosen key so local comparisons reveal global structure, such as neighboring overlapping ranges.

Ordered groupsSweep by start timeAdjacent duplicate checks

What makes it work

JavaScript's default sort is lexicographic; use a numeric comparator. Copy the input if mutation is not intended. Insertion Sort explicitly modifies its input.

When to choose something else

Sorting changes positional information unless original indices are retained. It may cost more than a single-pass hash solution.

See the pattern in action

Change the input. Follow the state. Make it yours.