|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// MARK: - SortDescriptorWrapper<T> |
| 4 | + |
| 5 | +/// A wrapper of `SortDescriptor<T>` that uses the native descriptor if available |
| 6 | +/// and fallbacks to `BackportSortDescriptor` otherwise. |
| 7 | +/// This will no longer be necessary if the minimum iOS version is bumped to iOS 17. |
| 8 | +struct SortDescriptorWrapper<T> { |
| 9 | + /// Comparator to use to sort. |
| 10 | + private let _compare: (T, T) -> ComparisonResult |
| 11 | + |
| 12 | + /// Initializes the sort descriptor. |
| 13 | + /// - Parameters: |
| 14 | + /// - keyPath: The key path to use to sort. |
| 15 | + /// - comparator: The comparator to use to sort the string. |
| 16 | + /// - order: The sort order to use. |
| 17 | + init( |
| 18 | + _ keyPath: KeyPath<T, String>, |
| 19 | + comparator: String.StandardComparator, |
| 20 | + order: SortOrder = .forward |
| 21 | + ) { |
| 22 | + if #available(iOS 17, *) { |
| 23 | + // Use native SortDescriptor on iOS 17+ |
| 24 | + let native = SortDescriptor(keyPath, comparator: comparator, order: order) |
| 25 | + _compare = { lhs, rhs in native.compare(lhs, rhs) } |
| 26 | + } else if #available(iOS 16, *) { |
| 27 | + let comparison = { (lhs: String, rhs: String) in |
| 28 | + comparator.compare(lhs, rhs) |
| 29 | + } |
| 30 | + // iOS 16 fallback using BackportSortDescriptor |
| 31 | + let backport = BackportSortDescriptor<T>(comparator: { lhs, rhs in |
| 32 | + let result = comparison(lhs[keyPath: keyPath], rhs[keyPath: keyPath]) |
| 33 | + return order == .forward ? result : result.reversed() |
| 34 | + }) |
| 35 | + _compare = backport.compare |
| 36 | + } else { |
| 37 | + // iOS 15 fallback using `localizedStandardCompare`. |
| 38 | + let backport = BackportSortDescriptor<T>(comparator: { lhs, rhs in |
| 39 | + let lhsStr = lhs[keyPath: keyPath] |
| 40 | + let rhsStr = rhs[keyPath: keyPath] |
| 41 | + let result = lhsStr.localizedStandardCompare(rhsStr) |
| 42 | + return order == .forward ? result : result.reversed() |
| 43 | + }) |
| 44 | + _compare = backport.compare |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Initializes the sort descriptor. |
| 49 | + /// - Parameters: |
| 50 | + /// - keyPath: The key path to use to sort. |
| 51 | + /// - comparator: The comparator to use to sort the string. |
| 52 | + /// - order: The sort order to use. |
| 53 | + init( |
| 54 | + _ keyPath: KeyPath<T, String?>, |
| 55 | + comparator: String.StandardComparator, |
| 56 | + order: SortOrder = .forward |
| 57 | + ) { |
| 58 | + if #available(iOS 17, *) { |
| 59 | + let native = SortDescriptor<T>(keyPath, comparator: comparator, order: order) |
| 60 | + _compare = { lhs, rhs in native.compare(lhs, rhs) } |
| 61 | + } else if #available(iOS 16, *) { |
| 62 | + let comparison: (String?, String?) -> ComparisonResult = { lhs, rhs in |
| 63 | + switch (lhs, rhs) { |
| 64 | + case let (lhsValue?, rhsValue?): |
| 65 | + return comparator.compare(lhsValue, rhsValue) |
| 66 | + case (nil, nil): |
| 67 | + return .orderedSame |
| 68 | + case (nil, _): |
| 69 | + return .orderedAscending |
| 70 | + case (_, nil): |
| 71 | + return .orderedDescending |
| 72 | + } |
| 73 | + } |
| 74 | + let backport = BackportSortDescriptor<T>(comparator: { lhs, rhs in |
| 75 | + let result = comparison(lhs[keyPath: keyPath], rhs[keyPath: keyPath]) |
| 76 | + return order == .forward ? result : result.reversed() |
| 77 | + }) |
| 78 | + _compare = backport.compare |
| 79 | + } else { |
| 80 | + // iOS 15 fallback using localizedStandardCompare |
| 81 | + let backport = BackportSortDescriptor<T>(comparator: { lhs, rhs in |
| 82 | + let lhsStr = lhs[keyPath: keyPath] |
| 83 | + let rhsStr = rhs[keyPath: keyPath] |
| 84 | + |
| 85 | + let result: ComparisonResult |
| 86 | + switch (lhsStr, rhsStr) { |
| 87 | + case let (lhsValue?, rhsValue?): |
| 88 | + result = lhsValue.localizedStandardCompare(rhsValue) |
| 89 | + case (nil, nil): |
| 90 | + result = .orderedSame |
| 91 | + case (nil, _): |
| 92 | + result = .orderedAscending |
| 93 | + case (_, nil): |
| 94 | + result = .orderedDescending |
| 95 | + } |
| 96 | + |
| 97 | + return order == .forward ? result : result.reversed() |
| 98 | + }) |
| 99 | + _compare = backport.compare |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Initializes the sort descriptor. |
| 104 | + /// - Parameters: |
| 105 | + /// - keyPath: The key path to use to sort. |
| 106 | + /// - ascending: Whether the order should be ascending. |
| 107 | + init<Value: Comparable>(_ keyPath: KeyPath<T, Value>, ascending: Bool = true) { |
| 108 | + if #available(iOS 17, *) { |
| 109 | + let native = SortDescriptor(keyPath, order: ascending ? .forward : .reverse) |
| 110 | + _compare = { lhs, rhs in native.compare(lhs, rhs) } |
| 111 | + } else { |
| 112 | + let backport = BackportSortDescriptor<T>(key: keyPath, ascending: ascending) |
| 113 | + _compare = backport.compare |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// Compares two values. |
| 118 | + func compare(_ lhs: T, _ rhs: T) -> ComparisonResult { |
| 119 | + _compare(lhs, rhs) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// MARK: - BackportSortDescriptor<T> |
| 124 | + |
| 125 | +/// Backport version of `SortDescriptor<T>` so it can be used on older than iOS 17 devices. |
| 126 | +struct BackportSortDescriptor<T> { |
| 127 | + /// Comparator to use to sort. |
| 128 | + private let comparator: (T, T) -> ComparisonResult |
| 129 | + |
| 130 | + /// Initializes the sort descriptor. |
| 131 | + /// - Parameter comparator: The comparator closure to use to sort. |
| 132 | + init(comparator: @escaping (T, T) -> ComparisonResult) { |
| 133 | + self.comparator = comparator |
| 134 | + } |
| 135 | + |
| 136 | + /// Initializes the sort descriptor. |
| 137 | + /// - Parameters: |
| 138 | + /// - key: The key path to use to sort. |
| 139 | + /// - ascending: Whether the order should be ascending. |
| 140 | + init<Value: Comparable>(key: KeyPath<T, Value>, ascending: Bool = true) { |
| 141 | + comparator = { lhs, rhs in |
| 142 | + let lhsValue = lhs[keyPath: key] |
| 143 | + let rhsValue = rhs[keyPath: key] |
| 144 | + if lhsValue == rhsValue { |
| 145 | + return .orderedSame |
| 146 | + } |
| 147 | + return ascending |
| 148 | + ? (lhsValue < rhsValue ? .orderedAscending : .orderedDescending) |
| 149 | + : (lhsValue > rhsValue ? .orderedAscending : .orderedDescending) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// Compares two values. |
| 154 | + func compare(_ lhs: T, _ rhs: T) -> ComparisonResult { |
| 155 | + comparator(lhs, rhs) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// MARK: - ComparisonResult |
| 160 | + |
| 161 | +extension ComparisonResult { |
| 162 | + /// Inverts the order of a `ComparisonResult`. |
| 163 | + func reversed() -> ComparisonResult { |
| 164 | + switch self { |
| 165 | + case .orderedAscending: |
| 166 | + return .orderedDescending |
| 167 | + case .orderedDescending: |
| 168 | + return .orderedAscending |
| 169 | + case .orderedSame: |
| 170 | + return .orderedSame |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// MARK: - Array extension |
| 176 | + |
| 177 | +extension Array { |
| 178 | + /// Sorts using descriptors. |
| 179 | + /// - Parameter descriptor: The descriptor to use to sort the array. |
| 180 | + /// - Returns: The sorted array. |
| 181 | + func sorted(using descriptor: SortDescriptorWrapper<Element>) -> [Element] { |
| 182 | + sorted(using: [descriptor]) |
| 183 | + } |
| 184 | + |
| 185 | + /// Sorts using descriptors. |
| 186 | + /// - Parameter descriptors: The descriptors to use to sort the array. |
| 187 | + /// - Returns: The sorted array. |
| 188 | + func sorted(using descriptors: [SortDescriptorWrapper<Element>]) -> [Element] { |
| 189 | + sorted { lhs, rhs in |
| 190 | + for descriptor in descriptors { |
| 191 | + let result = descriptor.compare(lhs, rhs) |
| 192 | + if result != .orderedSame { |
| 193 | + return result == .orderedAscending |
| 194 | + } |
| 195 | + } |
| 196 | + return false |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments