[swift-evolution] sortBy, minElementBy and maxElementBy methods

Tino Heth 2th at gmx.de
Thu Dec 31 06:14:38 CST 2015

> func byComparing<T, U: Comparable>(getComparisonKey: (T)->U) -> (T, T) -> Bool {
>   return { getComparisonKey($0) < getComparisonKey($1) }
> }
I've written something similar to bring file URLs into the order of their creation dates.
It is a small extension for collection types, and its only downside will disappear as soon as properties are accessible via method calls (afair there is a proposal in the making).

It was quite a lot fiddling with generics, and I don't have the tiny piece of code on my own computer, but it works in a way that you can do
let sorted = array.sortUsingAccessor(ElementType.methodThatReturnsComparable)
Beside the problems with properties, I really liked that approach.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151231/e1613803/attachment.html>

More information about the swift-evolution mailing list