Useful functions and extensions for sorting in Swift

Overview

SwiftSortUtils

CI Status Version License Platform

Motivation

This library takes a shot at making comparing and sorting in Swift more pleasant. It also allows you to reuse your old NSSortDescriptor instances in Swift.

Examples

let somePeople: [Person] = ...

// Sort by a single comparable attribute
let ... = somePeople.sort(by: compareBy(\.firstname))
let ... = somePeople.sort(by: compareBy { $0.firstname })
let ... = somePeople.sort(by: compareBy(.descending, \.firstname))
let ... = somePeople.sort(byComparing: \.firstname)
let ... = somePeople.sort(byComparing: \.firstname, ordering: .descending)

// Sort by multiple attributes
let ... = somePeople.sort(by:
  compareBy { $0.age } <|>
  compareBy { $0.lastname } <|>
  compareBy(\.firstname)
)

// With less cumbersome syntax:
let ... = somePeople.sort(by: \.age <|> \.firstname <|> \.lastname)
let ... = somePeople.sort(byComparing: [\.firstname, \.lastname]) // monomorphic

// Append any comparator function
let ... = somePeople.sort(by:
  compareBy { $0.age } <|>
  { (p1, p2) in p1.wearsGlasses() && !p2.wearsGlasses() }
)

// Reverse compare functions
let ... = somePeople.sort(by:
  compareBy(.descending) { $0.age } <|>
  compareBy { $0.lastname } <|>
  reverseComparator(compareBy(\.firstname)) // reverse any compare function
)

// Mix and match extractor and compare functions:
let ... = somePeople.sort(by:
  compareBy(.descending, \.age) <|>
  \.firstname <|>
  \.lastname <|>
  reverseComparator(myCompareFunction)
)

// Use an NSSortDescriptor
let ageSortDescriptor = NSSortDescriptor(key: "age", ascending: true)
let ... = somePeople.sort(ageSortDescriptor.toCompareFunction())

// Even Use multiple NSSortDescriptors
let nameSortDescriptors = [
  NSSortDescriptor(key: "lastname", ascending: true),
  NSSortDescriptor(key: "firstname", ascending: true)
]
let ... = somePeople.sort(by: nameSortDescriptors.toCompareFunction())

See the tests for more examples.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

Swift Version

This Version of SwiftSortUtils is meant to be used with Swift 5.

CocoaPods

SwiftSortUtils is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftSortUtils"

Manually

Download the files in Pod/Classes and drop them into your project.

Author

Daniel Strittmatter, [email protected]

License

SwiftSortUtils is available under the MIT license. See the LICENSE file for more info.

You might also like...
Swift-friendly API for a set of powerful Objective C runtime functions.
Swift-friendly API for a set of powerful Objective C runtime functions.

ObjectiveKit ObjectiveKit provides a Swift friendly API for a set of powerful Objective C runtime functions. Usage To use ObjectiveKit: Import Objecti

Infix operators for monadic functions in Swift
Infix operators for monadic functions in Swift

Indecipherable symbols that some people claim have actual meaning. Please see the documentation for installation instructions. What's included? Import

Call Swift functions from Rust with ease!

swift-rs Call Swift functions from Rust with ease! Todo Swift class deallocation from rust (implementing Drop and using deallocate_{type} methods) Mor

DGLabelSize - Functions that calculate the size of uilabel based on different string lengths
DGLabelSize - Functions that calculate the size of uilabel based on different string lengths

DGLabelSize Functions that calculate the size of uilabel based on different stri

An open source Instapaper clone that features apps and extensions that use native UI Components for Mac and iOS.
An open source Instapaper clone that features apps and extensions that use native UI Components for Mac and iOS.

TODO: Screenshot outdated Hipstapaper - iOS and Mac Reading List App A macOS, iOS, and iPadOS app written 100% in SwiftUI. Hipstapaper is an app that

A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

Extensions for Swift Standard Types and Classes

Cent Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. Dollar is a Swif

Collection of native Swift extensions to boost your development. Support tvOS and watchOS.
Collection of native Swift extensions to boost your development. Support tvOS and watchOS.

SparrowKit Collection of native Swift extensions to boost your development. Support iOS, tvOS and watchOS. If you like the project, don't forget to pu

Comments
  • Manual Installation

    Manual Installation

    CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

    It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

    opened by lfarah 1
Owner
Daniel Strittmatter
Daniel Strittmatter
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.

Features • Classes and Extensions Compatibility • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog • Exa

Fabrizio Brancati 992 Dec 2, 2022
Useful extensions for my Swift code

UIViewController extensions presentAlert(withTitle title: String, message : String) presentAlertDialog(withTitle title: String, message : String, acti

Bogdan Grafonsky 1 Oct 17, 2021
Personally useful Swift Extensions for iOS Development

Useful-Swift-Extensions Personally useful Swift Extensions for iOS Development; cobbled together from a variety of development projects and StackOverf

Nick Arner 5 Dec 13, 2021
Extendy - A set of useful string extensions.

Extendy A set of useful string extensions. Requirements iOS 11.0+ Swift 5+ Installation CocoaPods Extendy is available through CocoaPods. To install i

Anton Novichenko 3 Sep 23, 2021
A collection of useful result builders for Swift and Foundation value types

Swift Builders A collection of useful result builders for Swift and Foundation value types. Motivation Arrays, dictionaries, and other collection-base

David Roman 3 Oct 14, 2022
A set of helper classes and functions in Swift

SwiftTools This is a set of tools written in Swift that add some sugar and some small functionality. Mainly meant for small projects and scripts, as a

Vinicius Vendramini 0 Dec 13, 2021
BFKit is a collection of useful classes and categories to develop Apps faster.

Swift Version • What does it do • Language support • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog •

Fabrizio Brancati 806 Dec 2, 2022
A Collection of useful Swift property wrappers to make coding easier

Swift Property Wrappers A Collection of useful Swift property wrappers to make c

Gordan Glavaš 2 Jan 28, 2022
Functional data types and functions for any project

Swiftx Swiftx is a Swift library containing functional abstractions and extensions to the Swift Standard Library. Swiftx is a smaller and simpler way

TypeLift 219 Aug 30, 2022
Utility functions for validating IBOutlet and IBAction connections

Outlets Utility functions for validating IBOutlet and IBAction connections. About Outlets provides a set of functions which validate that IBOutlets ar

Ben Chatelain 129 May 2, 2022