         guard var match = anyIndex(of: element) else { return nil }
         // Walk backward to find the first matching element (in case there are duplicates)
         while let predecessor = index(match, offsetBy: -1, limitedBy: startIndex) {
-            if compare(lhs: self[predecessor], rhs: element) == .orderedSame {
+            if compare(self[predecessor], element) == .orderedSame {
                 match = predecessor
             } else {
                 break

            let mid = index(left, offsetBy: dist/2)
            let candidate = self[mid]

            switch compare(lhs: candidate, rhs: newElement) {
            case .orderedAscending:
                left = index(after: mid)
            case .orderedDescending:


// MARK: - Converting between a stdlib comparator function and Foundation.ComparisonResult
extension SortedArray {
    fileprivate func compare(_ lhs: Element, _ rhs: Element) -> Foundation.ComparisonResult {
        if areInIncreasingOrder(lhs, rhs) {
            return .orderedAscending
        } else if areInIncreasingOrder(rhs, lhs) {
