Is List.Sort() reliable when some values are equal?

I have a list of missions sorted by price; I’m using their index in the list to determine in PlayerPrefs whether they’ve been cleared. The catch is that some missions have the same price.

What’s the secondary means of sorting in this case? Will it reliably output the same order across launches and platforms?

The documentation from Microsoft on the C# language and libraries states the sort is unstable.

That’s CompSci speak for “no” to your question.

You can make it stable relative to your concerns by providing your own Comparison, then perform the comparison on more than one element of data to such an extent that no two items will be identical.

That requires that there be a stable, unique identifier. Only you would know what qualifies.

Sometimes the secondary (and occasionally several elements) are required. If you select elements that change over time, though, that would change the sorting order. Your stated requirement suggests you must choose elements that don’t change. Position, for example, is a bad choice for this.

If nothing else is available, you would have to consider creating a “serial number” for your data, so that every object gets a unique numeric (presumably) identifier, used only for that situation that “breaks the tie” for prices.

The comparison function would be slightly different than a default Comparison (which works on one field of data), and would look something like:

if ( x.price < y.price ) return -1;
if ( x.price > y.price ) return 1;
if ( x.serial < y.serial ) return -1;
if ( x.serial > y.serial ) return 1;
return 0;

This form can be extended to any number of “subfield” comparisons for sorting, in general usage. Some comparison functions in various languages only return greater or less than (relying on, say, not less than to mean greater or equal), which implies weak ordering. This interface requires strong ordering (separating equal from less or greater), but doesn’t imply stability. You’re “artificially” implementing stability relative to your data because you know there will be no two serial numbers that match in your data.