- Proposal: SE-XXXX
- Author: Charlie Monroe
- Status:
- Review manager: TBD
This proposal suggests introduction of a @sortable
attribute that may be
applied to properties.
Sorting is quite a frequent task that is performed quite a lot. In order
to maintain the sorting logic, often times it is convenient to create an extension
on Sequence
such as this one:
class Person {
var age: Int = 21
var firstName: String = "John"
var lastName: String = "Doe"
}
extension Sequence where Self.Iterator.Element: MyClass {
func sortedByFirstName() -> [Self.Iterator.Element] {
return self.sorted(isOrderedBefore: { $0.firstName < $1.firstName })
}
func sortedByLastName() -> [Self.Iterator.Element] {
return self.sorted(isOrderedBefore: { $0.lastName < $1.lastName })
}
func sortedByAge() -> [Self.Iterator.Element] {
return self.sorted(isOrderedBefore: { $0.age < $1.age })
}
}
This introduces a lot of boilerplate since a method needs to be implemented for each property by which one wants to define sorting.
This proposal suggests adding a @sortable
attribute that may be applied to
a property that conforms to Comparable
as follows:
class Person {
@sortable
var firstName: String = "John"
@sortable
var lastName: String = "Doe"
@sortable
var age: Int = 0
}
The compiler would then generate an extension on Sequence
, which is composed
as sorted<PropertyName>(ascending: Bool = true)
. The usage would look like this:
var people = [
Person(firstName: "John", lastName: "Doe", age: 15),
Person(firstName: "Jane", lastName: "Doe", age: 20),
]
people.sortByFirstName() // Jane Doe, John Doe
people.sortByAge() // John Doe, Jane Doe
people.sortByFirstName(ascending: false) // John Doe, Jane Doe
This addition could in the future lead to a Swift-like sort descriptors to be used by Interface Builder.
None since this is purely additive.
- Not implementing this at all.
- Allow
@sortable(ascending)
and@sortable(descending)
instead of a defaultascending
argument. - Instead of using
ascending: Bool
useascending: SortDirection
, whereSortDirection
is an enum with.ascending
and.descending
values.