# Why does Radix sort require stable digit sorts?

I'm reading the CLRS book and have a question about the following quote from the book.

In order for radix sort to work correctly, the digit sorts must be stable.

Why is stability required? Wouldn't Radix sort still produce correctly sorted output even if there was no consistency in ordering of identical values, since Radix sort sorts on every digit position?

• You can probably figure out the answer by simulating radix sort with a pencil and paper and trying to see where an unstable sort could mess things up. If you succeed with that, I encourage you to post it as an answer. – David Richerby Nov 17 '18 at 14:16

Let's sort [13, 12] using a LSD-first radix sort (base 10). But we'll use an unstable sort for each digit.
First we sort by the 1's place, so the array becomes [12, 13].
Now we sort by the 10's places, but 12 and 13 have the same digit. Since the sort is unstable, the resulting array could be [12, 13] or [13, 12]. We don't know.