<div dir="ltr">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.<div><br></div><div>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...</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 16, 2016 at 3:51 PM, Charlie Monroe via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi there,<br>
<br>
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.<br>
<br>
I've written a proposal draft here: <a href="https://gist.github.com/charlieMonroe/0752cd61f7937f714b689137daf9de21" rel="noreferrer" target="_blank">https://gist.github.com/<wbr>charlieMonroe/<wbr>0752cd61f7937f714b689137daf9de<wbr>21</a><br>
<br>
In brevity:<br>
<br>
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().<br>
<br>
Unfortunately, this isn't really clear under what key do the entities sort. For example:<br>
<br>
class Person {<br>
var age: Int = 21<br>
var firstName: String = "John"<br>
var lastName: String = "Doe"<br>
}<br>
<br>
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:<br>
<br>
extension Sequence where Self.Iterator.Element: MyClass {<br>
func sortedByFirstName() -> [Self.Iterator.Element] {<br>
return self.sorted(isOrderedBefore: { $0.firstName < $1.firstName })<br>
}<br>
func sortedByLastName() -> [Self.Iterator.Element] {<br>
return self.sorted(isOrderedBefore: { $0.lastName < $1.lastName })<br>
}<br>
func sortedByAge() -> [Self.Iterator.Element] {<br>
return self.sorted(isOrderedBefore: { $0.age < $1.age })<br>
}<br>
}<br>
<br>
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...<br>
<br>
Any thoughts on this?<br>
<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</blockquote></div><br></div></div>