# SE-0372: Document Sorting as Stable

**URL:** <https://forums.swift.org/t/se-0372-document-sorting-as-stable/60165>\
**Category:** Proposal Reviews\
**Tags:** stdlib\
**Created:** [September 7, 2022, 4:19pm UTC](https://forums.swift.org/t/se-0372-document-sorting-as-stable/60165 "2022-09-07T16:19:45Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [September 15, 2022, 4:20am UTC](https://forums.swift.org/t/se-0372-document-sorting-as-stable/60165/13 "2022-09-15T04:20:42Z")

</div>

> [@AlexanderM](#):
>
> I think it might first be more useful to explore some kind of compositional sort descriptor API, similar to how Foundation uses `NSArray<NSSortDescriptor>`.
> 
> If a need for stable sorting remains after that, then we can tackle it.

I absolutely agree that we should explore adding sort descriptors to the standard library! In addition to allowing you to compose multiple search criteria, as highlighted in your Stack Overflow answer, sort descriptors also streamline and eliminate errors in sorting based on a single property. We've had a variety of pitches over the years for this feature, and I look forward to working together to bring something compelling to the Swift Evolution community.

That said, sort stability is an important question that can be considered separately at the current time, without waiting for more exploration. In addition to the reasons described in the proposal (it would be disruptive to change even un-guaranteed behavior at this point, and brings benefits to those who would like to rely on sort stability), a stability guarantee is important to have for future work.

Simulating a composed sort is demonstration of stability, but the semantic guarantee is really about preserving a collection's _preexisting structure_. In your example of sorting by last name and then first name, whether by multiple searches, a carefully written predicate, or by some future sort descriptor, stability preserves the existing ordering of other fields in the data. That is, if the `contacts` array is already sorted by creation date, a stable sort preserves that order for contacts with exact matches across both names.

Thanks for your feedback!

> **The various pitches that I could find, sorted from oldest to newest**
>
> - [Sort Descriptors](https://forums.swift.org/t/sort-descriptors/4436)
> - [Sort `Collection` using `KeyPath`](https://forums.swift.org/t/sort-collection-using-keypath/14554)
> - [Sorting Collections with `map` closures and SortDescriptors](https://forums.swift.org/t/sorting-collections-with-map-closures-and-sortdescriptors/16276)
> - [Map Sorting](https://forums.swift.org/t/map-sorting/21421)
> - [Add sort overload to Sequence to sort by a Comparable attribute of elements](https://forums.swift.org/t/add-sort-overload-to-sequence-to-sort-by-a-comparable-attribute-of-elements/37683)

---

_[View the full topic](https://forums.swift.org/t/se-0372-document-sorting-as-stable/60165)._
