<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 2016-11-03, at 19:39, Chris Eidhof via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div dir="ltr" class=""><div class="">I was wondering if there's any interest in adding Swift-like sort descriptors to the language. Currently, we have `sort` and `sorted`, which take a function of type `(A, A) -> Bool`. Foundation has `NSSortDescriptor`, which corresponds more to `(A, A) -> ComparisonResult`. NSSortDescriptor is a lot easier to work with than Swift's stdlib way: it's more declarative, it's easy to combine multiple sort descriptors into one (e.g. sort by last name, then sort by first name, and so on). However, NSSortDescriptor uses runtime programming, and is not very typesafe.</div><div class=""><br class=""></div><div class="">We could do a lot better in Swift. There are at least a few possibilities:</div><div class=""><br class=""></div><div class="">typealias SortDescriptor<A> = (A, A) -> Bool</div><div class="">typealias SortDescriptor<A> = (A, A) -> ComparisonResult</div><div class="">struct SortDescriptor<A> { let compare: (A, A) -> Bool }</div><div class="">struct SortDescriptor<A> { let compare: (A, A) -> ComparisonResult }</div><div class=""><br class=""></div><div class="">I've experimented a bit with this, and it seems like the `struct` based version with `ComparisonResult` is quite nice, because then we can add methods/properties on it (e.g. to reverse a sort descriptor, or to return a `Bool` instead of a `ComparisonResult`). </div><div class=""><br class=""></div><div class="">My "SortDescriptor" could also be called "Comparator", and in fact, there is already something like that in Foundation (of type (Any,Any) -> Bool).</div><div class=""><br class=""></div><div class="">Is there interest in adding support for this to the stdlib? If yes, is anyone interested in writing a good proposal together?</div></div></div></blockquote><div><br class=""></div><div>There is definitely interest!</div><div><br class=""></div><div>In fact, Robert Widmann, Jaden Geller, Harlan Haskins and Pyry Jahkola had previously worked on a draft proposal to clean up Comparable's semantics, and to move from a < + == model to a single comparator operator called "<=>”. Sadly this happened just before Swift 3 deadline, and the proposal was lost in the noise.</div><div><br class=""></div><div><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025466.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025466.html</a></div><div><a href="https://github.com/pyrtsa/swift-evolution/blob/ca89e7b3a1dffc99baa695a03544fcba75afd0f3/proposals/NNNN-formalized-ordering.md" class="">https://github.com/pyrtsa/swift-evolution/blob/ca89e7b3a1dffc99baa695a03544fcba75afd0f3/proposals/NNNN-formalized-ordering.md</a></div><div><br class=""></div><div>The new sort functions are included under Future Directions above, but it would also be possible to add them before resolving the general case. </div><div><br class=""></div><div>If this is a good time to reopen this discussion, then I’m all for it! </div><div><br class=""></div><div>-- </div><div>Karoly</div><div><br class=""></div></div></body></html>