A sort is said to be stable if it maintains the relative order of elements with equal keys. I guess my question is really, what is the benefit of maintaining this relative o
Let's say you are sorting on an input set which has two fields, and, you only sort on the first. The '|' character divides the fields.
In the input set you have many entries, but, you have 3 entries that look like
. . . AAA|towing . . . AAA|car rental . . . AAA|plumbing . . .
Now, when you get done sorting you expect all the fields with AAA in them to be together.
A stable sort will give you: . . . AAA|towing AAA|car rental AAA|plumbing . . .
ie, the three records which had the same sort key, AAA, are in the same order in the output that they were in the input. Note that they are not sorted on the second field, because you didn't sort on the second field in the record.
An unstable sort will give you: . . . AAA|plumbing AAA|car rental AAA|towing . . .
Note that the records are still sorted only on the first field, and, the order of the second field differs from the input order.
An unstable sort has the potential to be faster. A stable sort tends to mimic what non-computer scientist/non-math folks have in their mind when they sort something. Ie, if you did an insertion sort with index cards you would most likely have a stable sort.