← Back to Blog

Make the Largest Number - Custom Comparator Sorting by Concatenation - AlgoNote

Largest NumberSortingCustom ComparatorStringCoding TestAlgoNote

Build the largest number by gluing pieces

Picture a small license-plate shop. In a box are metal pieces with numbers engraved on them: [3, 30, 34, 5, 9]. A customer says: "Glue all of these pieces into one line and make the biggest possible number!"

In what order should the pieces be joined to form the largest number?


The trap — "by numeric value" is wrong

The first instinct is "put the bigger numbers first." Sorting by numeric value descending gives 34, 30, 9, 5, 33430953.

It looks plausible, but it is the wrong answer. The correct answer is 9534330.

Why? 9 is a smaller number than 34, but putting 9 at the front makes the leading digit 9, which makes the whole number far larger. So the real key is not "a piece's numeric value" but "which side is bigger when glued."

The tricky pair is 3 and 30. Which goes first?

  • 3 first: "3" + "30" = 330
  • 30 first: "30" + "3" = 303

Since 330 > 303, 3 must come first. Numerically 30 is bigger, but once glued, 3 has to come first to make a larger number.


The core idea — a+b vs b+a

This is where the sorting insight appears. To decide the order of two pieces a and b, compare the concatenated strings, not the numeric values.

  • If a + b > b + aa goes first
  • If b + a > a + bb goes first

Plugging this one-line rule in as the sort comparator is the entire trick.

Why is string comparison correct? a+b and b+a always have the same length (the combined length of both pieces). For two numbers of equal length, you compare from the leading digit, which is exactly what lexicographic string comparison does.

Test a few pairs and the rule clicks.

  • 5 vs 959 vs 95 → 95 wins, so 9 first
  • 34 vs 5345 vs 534 → 534 wins, so 5 first
  • 30 vs 343034 vs 3430 → 3430 wins, so 34 first

Sorting descending with this comparison yields the order 9, 5, 34, 3, 30, and gluing them all gives 9534330 — the answer.

One last trap. If the pieces are all zeros ([0, 0]), the concatenation becomes "00", but the answer should be a single "0". If the first character after sorting is '0', just return "0" and it is handled cleanly.

In short, the flow is:

1. Convert every piece to a string

2. Sort descending by comparing a+b vs b+a

3. Concatenate the sorted pieces in order

4. If the front is '0', return just "0"


See it with your own eyes

We built the entire process as a step-by-step visualization: pieces go through a+b vs b+a comparisons (yellow), swap positions (red), and lock into their final order one slot at a time (green). At each comparison you can see exactly how strings like "330" vs "303" decide the winner.

👉 Make the Largest Number — watch the step-by-step visualization

"Why must 3 come before 30?" is confusing in text, but once you watch the two strings compared side by side, "ah, the bigger glued side wins" lands instantly.

Make the Largest Number - Custom Comparator Sorting by Concatenation - AlgoNote - AlgoNote | 알고노트(AlgoNote)