import Foundation
A convenient extension to the Sequence protocol that allows you to sort elements in a Swift-like way. It is inspired by the SortDescriptor implementation in iOS >= 15, and the Swift by Sundell article on sorting Swift collections. If your minimum project target operating system version is iOS >= 15, you should use the Apple-proprietary SortDescriptor.
struct SomeSortableStructure {
let id: Int
let isSelected: Bool
let title: String
let description: String?
}
struct SorterGuy {
func sort(_ items: [SomeSortableStructure]) -> [SomeSortableStructure] {
items.sorted(using: [
.keyPath(\.title, order: .ascending),
.keyPath(\.isSelected, order: .descending),
.keyPath(\.description, order: .ascending),
.keyPath(\.id, order: .ascending)
])
}
}
The implementation is quite compact so in most cases it can be just copied to the target project.
dependencies: [
.package(url: "https://github.com/Hydralo/SwiftKeyPathSortingRetrofit.git")
]
- iOS 10+