Skip to content

Commit 2e3c3a7

Browse files
committed
Add sorted() function
1 parent 322d9ff commit 2e3c3a7

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Sources/IdentifiedCollections/IdentifiedArray/IdentifiedArray+MutableCollection.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ extension IdentifiedArray: MutableCollection {
117117
try self._dictionary.sort(by: { try areInIncreasingOrder($0.value, $1.value) })
118118
}
119119

120+
/// Returns a copy of the collection, sorted using the given predicate as
121+
/// the comparison between elements.
122+
///
123+
/// When you want to sort a collection of elements that don't conform to the `Comparable`
124+
/// protocol, pass a closure to this method that returns `true` when the first element should be
125+
/// ordered before the second.
126+
///
127+
/// Alternatively, use this method to sort a collection of elements that do conform to
128+
/// `Comparable` when you want the sort to be descending instead of ascending. Pass the
129+
/// greater-than operator (`>`) operator as the predicate.
130+
///
131+
/// `areInIncreasingOrder` must be a *strict weak ordering* over the elements. That is, for any
132+
/// elements `a`, `b`, and `c`, the following conditions must hold:
133+
///
134+
/// * `areInIncreasingOrder(a, a)` is always `false`. (Irreflexivity)
135+
/// * If `areInIncreasingOrder(a, b)` and `areInIncreasingOrder(b, c)` are both `true`, then
136+
/// `areInIncreasingOrder(a, c)` is also `true`. (Transitive comparability)
137+
/// * Two elements are *incomparable* if neither is ordered before the other according to the
138+
/// predicate. If `a` and `b` are incomparable, and `b` and `c` are incomparable, then `a`
139+
/// and `c` are also incomparable. (Transitive incomparability)
140+
///
141+
/// The sorting algorithm is not guaranteed to be stable. A stable sort preserves the relative
142+
/// order of elements for which `areInIncreasingOrder` does not establish an order.
143+
///
144+
/// - Parameter areInIncreasingOrder: A predicate that returns `true` if its first argument should
145+
/// be ordered before its second argument; otherwise, `false`. If `areInIncreasingOrder` throws
146+
/// an error during the sort, the elements may be in a different order, but none will be lost.
147+
/// - Complexity: O(*n* log *n*), where *n* is the length of the collection.
148+
/// - Returns: A sorted copy of this collection.
149+
@inlinable
150+
public func sorted(
151+
by areInIncreasingOrder: (Element, Element) throws -> Bool
152+
) rethrows -> Self {
153+
var copy = self
154+
try copy.sort(by: areInIncreasingOrder)
155+
return copy
156+
}
157+
120158
/// Exchanges the values at the specified indices of the array.
121159
///
122160
/// Both parameters must be valid indices below ``endIndex``. Passing the same index as both `i`
@@ -150,4 +188,22 @@ extension IdentifiedArray where Element: Comparable {
150188
public mutating func sort() {
151189
self.sort(by: <)
152190
}
191+
192+
/// Returns a sorted copy of the collection.
193+
///
194+
/// You can sort an ordered set of elements that conform to the `Comparable` protocol by calling
195+
/// this method. Elements are sorted in ascending order.
196+
///
197+
/// To sort the elements of your collection in descending order, pass the greater-than operator
198+
/// (`>`) to the ``sort(by:)`` method.
199+
///
200+
/// The sorting algorithm is not guaranteed to be stable. A stable sort preserves the relative
201+
/// order of elements that compare equal.
202+
///
203+
/// - Complexity: O(*n* log *n*), where *n* is the length of the collection.
204+
/// - Returns: A sorted copy of this collection.
205+
@inlinable
206+
public func sorted() -> Self {
207+
self.sorted(by: <)
208+
}
153209
}

0 commit comments

Comments
 (0)