SwiftKeyPathSortingRetrofit

main

convenient extension to the Sequence protocol that allows you to sort elements in a Swift-like way
Hydralo/SwiftKeyPathSortingRetrofit

import Foundation

Swift KeyPath Sorting Retrofit

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.

License: MIT

Example

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)
        ])
    }
}

Installation

The implementation is quite compact so in most cases it can be just copied to the target project.

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/Hydralo/SwiftKeyPathSortingRetrofit.git")
]

Requirements

  • iOS 10+

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Mar 20 2024 14:55:14 GMT-0900 (Hawaii-Aleutian Daylight Time)