[swift-evolution] [Discussion] Sortable Attribute

Xiaodi Wu xiaodi.wu at gmail.com
Tue Aug 16 17:22:44 CDT 2016

A core team-driven proposal that didn't get sufficiently refined before the
Swift 3 cutoff was to refine Equatable and Comparable. The direction that
the core team was going was this: if your class is Comparable, then it
defines a total order, and `sort()` and `sorted()` are to be a stable sort
based on that total order.

Now, as to sorting based on particular properties, Dave has implemented the
renaming of predicate labels so it's not so onerous anymore to write
`self.sorted { $0.age < $1.age }` or even `self.sorted(by: { $0.age <
$1.age })`. Why are you generating your own wrapper functions for these at
all? Once refinements to Equatable and Comparable go in, it's easy to see
how one might have a syntax where you might have `self.sorted(.ascending,
by: { $0.age })`. So I guess, I don't know that this sugar is gaining too
much because I don't see the motivation for generating these wrapper
methods at all...

On Tue, Aug 16, 2016 at 3:51 PM, Charlie Monroe via swift-evolution <
swift-evolution at swift.org> wrote:

> Hi there,
> I'm not really sure whether this is appropriate, considering that Swift 4
> should be basically focusing purely on ABI compatibility and a few other
> issues mentioned by Chris, but I've been thinking about this for some time
> and would make a lot of lives easier, so it could be part of 3.x.
> I've written a proposal draft here: https://gist.github.com/charlieMonroe/
> 0752cd61f7937f714b689137daf9de21
> In brevity:
> Currently, if you want consistent sorting of some entities, you either
> declare them Comparable and decide under which key do they sort and then
> use entities.sorted().
> Unfortunately, this isn't really clear under what key do the entities
> sort. For example:
> class Person {
>     var age: Int = 21
>     var firstName: String = "John"
>     var lastName: String = "Doe"
> }
> If this were Comparable, would it sort by first name, last name or age?
> What if you wanted to be able to sort using all the properties? You'd
> probably resort to writing an extension on sequence:
> 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 is fairly tedious, however. What I propose is to add a @sortable
> attribute to properties, which would automatically generate the code for
> you. It - of course - requires the property to conform to Comparable...
> Any thoughts on this?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160816/5da04a50/attachment.html>

More information about the swift-evolution mailing list