Algorithms ⟩ Sorting ⟩
seq.sorted()
seq.sorted(by:)
seq.sorted(_:) - sort by keypaths with ascending/descending orders
the sorted(by:) function is actually 2 different functions.
sorted(by:)
one requires Comparable and uses < internally.
Comparable
<
the other lets you specify the sorting logic directly (Comparable conformance not required).
... this convenience sorting by keyPath would still require 2 functions.
keyPath
👉 Reference default comparison function as a function parameter
Sundell ⟩ Sorting Swift collections ⭐️
Sarun ⟩
Different ways to sort an array of strings in Swift
How to sort by multiple properties in Swift
Standard Lib ⟩ Collections ⟩ Sequence
sorted() - Element: Comparable
Swift ⟩ Array ⟩
sort() - sort in place. Element: Comparable
sort(by:) - sort in place.
Comparable Enums
Last updated 3 years ago
Was this helpful?