What are Sorting Techniques?
Sorting arranges items according to a comparison rule. The useful order may be numeric, alphabetical, grouped by category, or defined by the final output itself.
You need arrays, comparisons, and basic time complexity. The short version is: state what it means for one item to come before another, then choose the simplest algorithm that preserves that rule.
The Core Idea: Define "Before"
In Largest Number, ordinary numeric order is wrong.
For 3 and 30, compare the strings 330 and 303.
Because 330 is larger, 3 must appear before 30.
Why compare both concatenations? Those are the only two possible relative orders for the pair, and the larger one directly improves the final number.
It is like arranging word tiles to make the alphabetically largest combined label. Each pair must be ordered by how the two complete arrangements compare.
How the Custom Comparator Decides
The Rule You Keep True
The sorted prefix or partition must satisfy the chosen ordering rule for every item already placed there. A comparator must give the same answer whenever it sees the same pair and must not create circular ordering.
Different input structure supports different techniques:
- General comparison sort works when only pairwise order is known.
- Custom comparator sort orders objects or strings by the requested final result.
- Three-way partition groups a fixed set of categories in one pass.
- Interval sorting orders by a selected endpoint before a scan.
Largest Number is the one that usually trips us up. The values look numeric, but their relative order depends on concatenated strings. Testing 3 against 30 makes the rule much clearer than trying to invent a digit-by-digit shortcut.
Picking the Right Shape
From Rule to Code
- Write the ordering rule using two concrete items.
- Decide whether a general sort, custom comparator, or fixed-category partition fits the input.
- Apply the comparison consistently whenever items are moved.
- Preserve original indexes or stability only when the result needs them.
- Handle final formatting, such as leading zeroes, after the order is complete.
General comparison sorting takes O(n log n) time, while fixed-category partitioning can take O(n) time and O(1) space.
When to use it
- •The answer becomes easier once values are placed in a useful order.
- •The prompt asks for a custom ranking rather than ordinary numeric order.
- •Equal categories must be grouped or moved in place.
- •A later algorithm requires sorted input.
How to identify it
- •The output is an ordered version of the input.
- •The important decision is how two items should compare.
- •The prompt asks for the largest concatenated number, color grouping, or ranking by a property.
- •Sorting turns scattered related values into neighboring values.
Key insights
- →A comparator defines which of two items must appear first.
- →The comparator must be consistent across every pair or a general sort cannot behave predictably.
- →Largest Number compares `a + b` with `b + a`, not `a` with `b` numerically.
- →Sort Colors uses three regions because only three categories exist.
- →Comparison sorting has an **O(n log n)** lower bound in the general case.
Common mistakes
- 01Returning a boolean from a JavaScript comparator instead of a negative, zero, or positive number.
- 02Comparing Largest Number values numerically and missing the best concatenation order.
- 03Using a stable-order assumption without checking whether the runtime guarantees stability.
- 04Sorting when the problem needs original indexes and not preserving them.
- 05Using a general comparison sort when a tiny fixed set of categories permits a simpler linear pass.