# Sorting Collections with \`map\` closures and SortDescriptors

**URL:** <https://forums.swift.org/t/sorting-collections-with-map-closures-and-sortdescriptors/16276>\
**Category:** Pitches\
**Created:** [September 18, 2018, 7:41pm UTC](https://forums.swift.org/t/sorting-collections-with-map-closures-and-sortdescriptors/16276 "2018-09-18T19:41:09Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [September 19, 2018, 1:21am UTC](https://forums.swift.org/t/sorting-collections-with-map-closures-and-sortdescriptors/16276/7 "2018-09-19T01:21:39Z")

</div>

> [@ben-cohen](#):
>
> ```swift
> people.sort(by: [
> { $0.age < $1.age },
> { $0.name < $1.name }
> ])
> 
> ```

That's a great suggestion that I hadn't thought of before! I _ **completely** _ prefer that design over the design I proposed initially. It fits in much better with the existing standard library, and it avoids having to add a new type.

My dream syntax would be:

```swift
people.sorted(by: [
    { $0.name },
    { $0.age }
])

```

but we can't express that in the type system without variadic generics. The main reason I've been using a `SortDescriptor` type is to type-erase the `map` closures.

I'm still very keen on the `map`-closure design, though. For symmetry, that could potentially give us four designs:

```swift
// (1) The current standard library, sort(by: (Element, Element) -> Bool)
people.sort(by: { $0.age < $1.age })

// (2) A map-closure design, sort<T: Comparable>(by: (Element) -> T)
people.sort(by: { $0.age })

// (3) An improved "Sort Descriptor" design: sort(by: [(Element, Element) -> Bool])
people.sort(by: [
    { $0.age < $1.age },
    { $0.name < $1.name }
])

// (4) An additional design for symmetry, that would require Variadic Generics.
// Not currently possible.
people.sort(by: [
    { $0.age },
    { $0.name }
])

```

So that being said, there are a few open questions:

- Is it acceptable to have **(2)** and **(3)** but not **(4)**? That would feel a bit asymetric to me.

- If this was packaged together as a comprehensive "sort ergonomics" proposal, would it make sense to also conditionally propose **(4)** (the design that requires variadic generics) on the precondition that it would be implemented if/when it became possible?

- Is it even desirable to have _four_ overloads of `sort(by:)`? If not, which one do we cut? I think **(3)** is filling a bigger gap / solving a more important problem, while **(2)** leans closer towards "sugar". That's probably a strong indicator, but I would also be sad to miss out on **(2)** if we cut it out.

Of all of those options, I think the most sensible choice may potentially be to slim the proposal to just include `sort(by: [(Element, Element) -> Bool])`.

---

_[View the full topic](https://forums.swift.org/t/sorting-collections-with-map-closures-and-sortdescriptors/16276)._
