A stable sort keeps the relative order of elements with equal keys. It matters when you sort by multiple fields (e.g., sort by last name, then by first name).
A sort is *stable* if elements with equal sort keys keep their original relative order. Stability doesn’t change whether the final sequence is correctly ordered by the key; it changes what happens inside ties.
Input: (dept=A, name=Z), (dept=A, name=B)
Sort by dept only:
- Stable => Z stays before B (same as input)
- Unstable => the two may swap