[swift-evolution] Add stableSort() to the standard library.

Janosch Hildebrand jnosh at jnosh.com
Mon Jan 11 08:57:46 CST 2016

Strong +1 for adding a stable sort to the stdlib.

> Should we also provide a stableSortInPlace() implementation?


> On 11 Jan 2016, at 04:39, Dmitri Gribenko via swift-evolution <swift-evolution at swift.org> wrote:

>> ####
>> Alternatives considered
>> ####
>> An alternative would be that of augmenting sort() to get a Bool as argument
>> (stable: True). I'd rather like better to mirror what almost every
>> other programming
>> language does (providing a separate API).
>> It's an API breaking change (I think, but I'm not entirely sure).
> I have been thinking about this, and even though other languages it is
> a separate function, since the fundamental operation of sort() and
> stableSort() seems sufficiently similar to me, the defaulted parameter
> seems like the right thing to do.  What other parameters could sort()
> conceivably get in future?  Should the parameter be a boolean flag or
> a resilient option set?  Foundation has NSSortOptions, which has just
> "Stable" and "Concurrent".  The "concurrent" part, in my opinion,
> needs to be handled somehow for all algorithms in a consistent manner,
> rather than adding flags for each API separately.

This seems like the right direction to me.

Are there other compelling options for 'sort' though?
More input towards the sort strategy/algorithm(s) employed is what I can think of but that has relatively limited applicability.
Then again this is going to exist anyway whether it is in the stdlib or not...

Personally I wouldn't mind `foo.sort(.stable)` so I'm pro option set if there is a reasonable case for keeping our options open, so to speak.

- Janosch

More information about the swift-evolution mailing list