Cotton code
20 lines · 918 B · SwiftRaw
// A sort that keeps the order of the elements it calls equal. Swift's `sort`// is not stable, and an order that leans on "as read" for its ties (the order// a store answered in, `created_at`) would otherwise shuffle equal rows on// every read.extension Array {    /// Sort, keeping the order of elements the comparator calls equal.    ///    /// `areInIncreasingOrder` must be a STRICT ordering (`<`, never `<=`), as    /// `sorted(by:)` requires; ties fall through to the original index.    func stableSorted(by areInIncreasingOrder: (Element, Element) -> Bool) -> [Element] {        enumerated()            .sorted { left, right in                if areInIncreasingOrder(left.element, right.element) { return true }                if areInIncreasingOrder(right.element, left.element) { return false }                return left.offset < right.offset            }            .map(\.element)    }}