mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
18 lines
359 B
Swift
18 lines
359 B
Swift
// RUN: %target-swift-frontend -typecheck %s
|
|
|
|
extension Sequence {
|
|
func sorted<T: Comparable, K: KeyPath<Element, T>>(by keyPath: K) -> Array<Element> {
|
|
self.sorted { $0[keyPath:keyPath] < $1[keyPath:keyPath] }
|
|
}
|
|
}
|
|
|
|
struct Foo {
|
|
let a: Int
|
|
}
|
|
|
|
func main() {
|
|
print([Foo(a: 2), Foo(a:1), Foo(a:4), Foo(a:3)].sorted(by: \Foo.a))
|
|
}
|
|
|
|
main()
|