Skip to content

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

License

Notifications You must be signed in to change notification settings

Hydralo/SwiftKeyPathSortingRetrofit

Repository files navigation

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+

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages