# Sort(by:) min(by:) max(by:) with keyPaths

**URL:** <https://forums.swift.org/t/sort-by-min-by-max-by-with-keypaths/38976>\
**Category:** Discussion\
**Created:** [August 1, 2020, 6:08pm UTC](https://forums.swift.org/t/sort-by-min-by-max-by-with-keypaths/38976 "2020-08-01T18:08:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lovee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lovee/32/37957_2.png) [@lovee](https://forums.swift.org/u/lovee)\
**Post date:** [August 1, 2020, 6:08pm UTC](https://forums.swift.org/t/sort-by-min-by-max-by-with-keypaths/38976/1 "2020-08-01T18:08:10Z")

</div>

4 years ago there was a similar discussion:

> [@sortBy, minElementBy and maxElementBy methods](https://forums.swift.org/t/sortby-minelementby-and-maxelementby-methods/855):
>
> Consider the follows: struct Person { &nbsp;&nbsp;&nbsp;&nbsp;var name: String &nbsp;&nbsp;&nbsp;&nbsp;var age: Int } let peoples = [Person(name: "Hawk", age: 24), Person(name: "Andrew", age: 23 )] let youngest = peoples.minElement { $0.age \< $1.age } print(youngest?.name) it's silly that we always have to write the code like { $0.some \< $1.some } or { some($0) \< some($1) } so, we should add those methods to stdlib: extension SequenceType { &nbsp;&nbsp;&nbsp;&nbsp;/// Returns the minimum element in `self` or `nil` if the sequence is empty. …

Now 4 years passed and now we've got the new KeyPath feature, but we still don't have a convenient method to sort a sequence, or get the max / min element by its elements' properties

For example currently if we want to get the greatest element from a sequence according to a property of its elements, with the standard library the only way is to write a customized closure to compare 2 elements like this:

```
let max = sequence.max(by: { $0.property < $1.property })

```

Apart from inconvenience, there are 2 major issues with the API from my perspective:

1. The purpose of the closure's return value is not clear, which conflicts with Swift's fundamental API Design Guidelines. What does the Bool return value mean? If you can come up with the idea that it's sorting the array and it will return the last element of the array as the max value, then maybe you can somehow get the idea of "sorting the sequence, by returning `true` if $0's property is less than $1's property", but most people who tried their first time don't.

2. Duplicates of information, which mis-typing could easily cause bugs. For example:

Both statements above are logically bugs, but they can't be checked at build time, which means it's not safe, and it conflicts with Swift's safety goal.

And since we already have keyPath feature in Swift, why couldn't we write statements like below?

```
let max = sequence.max(by: \.property)

```

This is much safer, much more clear, and much more developer-friendly comparing with existing methods.

Also, if we could have a method like

```
let max = sequence.max(by: { $0.getSomeValue(from: anotherData) })

```

then it could also be very convenient.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 1, 2020, 6:50pm UTC](https://forums.swift.org/t/sort-by-min-by-max-by-with-keypaths/38976/2 "2020-08-01T18:50:46Z")

</div>

This idea was discussed most comprehensively in this pitch:

> [@Map Sorting](https://forums.swift.org/t/map-sorting/21421):
>
> Map Sorting Authors: [Anthony Latsis](https://github.com/AnthonyLatsis), [Cal Stephens](https://github.com/calda) Status: Awaiting Review Implementation: [apple/swift#23156](https://github.com/apple/swift/pull/23156)Introduction This proposal presents an addition to the Standard Library that makes it easy to sort a collection over some set of mapped values, provided via a transform closure or KeyPath, in a way that is ergonomic, idiomatic, and performant. Motivation To sort a Collection, you're currently required to specify an areInIncreasingOrder predicate of the form (Element, Element) -\> B…

At the bottom, the authors summarize some guidance from Ben Cohen regarding the path forward for sorting-related proposals. It would be good for any further discussion of this topic to pick up from there, and how/if things have changed in the past year, and what the next steps should be for considering sorting issues more broadly!

---

<div class="post-metadata">

**Author:** ![lovee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lovee/32/37957_2.png) [@lovee](https://forums.swift.org/u/lovee)\
**Post date:** [August 1, 2020, 6:54pm UTC](https://forums.swift.org/t/sort-by-min-by-max-by-with-keypaths/38976/3 "2020-08-01T18:54:36Z")

</div>

Thanks! I searched the forum with `min max` keywords but I missed `sorted` 😅
