The documentation of the current Sort.swift still states that
/// The sorting algorithm is not guaranteed to be stable
On the other hand, the actual implementation calls
/// Sorts the elements of this buffer according to `areInIncreasingOrder`,
/// using a stable, adaptive merge sort.
///
/// The adaptive algorithm used is Timsort, modified to perform a straight
/// merge of the elements using a temporary buffer.
@inlinable
public mutating func _stableSortImpl(
by areInIncreasingOrder: (Element, Element) throws -> Bool
) rethrows { ... }
Does that mean that sort()
is stable in Swift 5? I could not find that documented in the CHANGELOG or in an evolution proposal.
Timsort is mentioned as a possible choice for a stable sort algorithm in [Starter Proposal] Add Stable Sort to StdLib, but that discussion ended in March '18.
There is also SR-306 Add stable sort algorithm, which is still in the Open state.
Regards, Martin