#T21SortingDescriptor
Helper type to use Sorting Descriptors in an easy way.
T21SortingDescriptorSwift provides a helper type to use Sorting Descriptors in an easy way. Sorting Descriptors can also be combined to create different sorting levels.
Swift standard language does not include the type SortDescriptor. We may use the Objective-C API NSSortDescrptor class, but this will lead into having to use NSArray for our collections (leading to ugly casts in the code and other stuff).
T21SortingDescriptorSwift is available through Carthage, CocoaPods or Swift Package Manager.
To install T21SortingDescriptorSwift with Carthage, add the following line to your Cartfile
.
github "worldline-spain/T21SortingDescriptorSwift"
Then run carthage update --no-use-binaries
command or just carthage update
. For details of the installation and usage of Carthage, visit its project page.
To install T21SortingDescriptorSwift with CocoaPods, add the following lines to your Podfile
.
source '/~https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'T21SortingDescriptorSwift'
Then run pod install
command. For details of the installation and usage of CocoaPods, visit its official website.
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
.
.package(url: "/~https://github.com/worldline-spain/T21SortingDescriptorSwift.git", .upToNextMajor(from: "2.3.0"))
For details of the installation and usage of Swift Package Manager, visit its official website.
Create a simple SortDescriptor using a first function parameter to retrieve the desired key to compare, and then the comparing function as the second parameter. This approach gives a lot of freedom when fetching the keys from the objects.
In the following example we are sorting an array of Persons:
public class Person {
public var age: Int
public var name: String
public init(_ age: Int, _ name: String) {
self.age = age
self.name = name
}
}
let personsToSort = [Person(29,"Tom"),Person(10,"Jack"),Person(54,"Ana"),Person(24,"Maria")]
let sortingDescriptor: SortDescriptor<Person> = sortDescriptor({ $0.age })
let personsSortedByAge = personsToSort.sorted(by: sortingDescriptor)
/* Output
Jack: 10
Maria: 24
Tom: 29
Ana: 54
*/
In the following example we are combining 2 different sorting descriptors to apply 2 level sorting.
let personsToSort = [Person(29,"Tom"),Person(10,"Jack"),Person(54,"Ana"),Person(24,"Maria"),Person(24,"Aurora"),Person(29,"Daniel")]
let sortingDescriptorByAge: SortDescriptor<Person> = sortDescriptor({ $0.age }, >)
let sortingDescriptorByName: SortDescriptor<Person> = sortDescriptor(key: { $0.name }, String.localizedCaseInsensitiveCompare)
let combinedSortingDescriptor = combine(sortDescriptors: [sortingDescriptorByAge,sortingDescriptorByName])
let personsSortedByAgeAndName = personsToSort.sorted(by: combinedSortingDescriptor)
/* Output
Ana: 54
Daniel: 29
Tom: 29
Aurora: 24
Maria: 24
Jack: 10
*/
- Eloi Guzman Ceron - Initial work
- Edwin Peña - Initial work
- Salvador Martin - Initial work
- Marcos Molero - Swift 5 integration, Carthage and Swift Package Manager integration
- Anan Sadiya - Swift 5 integration, Carthage and Swift Package Manager integration
See also the list of contributors who participated in this project.
This project is licensed under the Apache 2.0 License - see the LICENSE.md file for details
- To Worldline Spain iOS development team.