RandomAccessCollection

protocol RandomAccessCollection : _RandomAccessIndexable, BidirectionalCollection where Self.Indices : BidirectionalCollection, Self.Indices : _RandomAccessIndexable, Self.SubSequence : BidirectionalCollection, Self.SubSequence : _RandomAccessIndexable

The solution is inspired by the comment https://stackoverflow.com/a/45585365/7926205

  • return a sorted collection this use a stable sort algorithm

    Declaration

    Swift

    public func stableSorted(by areInIncreasingOrder: (Iterator.Element, Iterator.Element) -> Bool?) -> [Iterator.Element]

    Parameters

    areInIncreasingOrder

    return nil when two element are equal

    Return Value

    the sorted collection