# Wanted: an API for merging sorted collections

**URL:** <https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722>\
**Category:** General Usage\
**Tags:** sort, collection, sorting\
**Created:** [June 16, 2024, 6:23pm UTC](https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722 "2024-06-16T18:23:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 16, 2024, 6:23pm UTC](https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722/1 "2024-06-16T18:23:47Z")

</div>

I don’t think there’s a public function for the merge algorithm anywhere in Julia or the ecosystem. It’s what merge sort is based on. Wikipedia introduces merging like this:

> **Merge algorithms** are a family of [algorithms](https://en.wikipedia.org/wiki/Algorithm) that take multiple [sorted](https://en.wikipedia.org/wiki/Sorting_algorithm) lists as input and produce a single list as output, containing all the elements of the inputs lists in sorted order.

I guess it’d be nice to have an interface package providing merging APIs. We could, among other things, distinguish between:

1. stable and unstable merging (stable merging is the most well-known, but oblivious merging is an example of unstable merging, FTR)
2. mutating (`merged!`?) and copying (`merged`?) functions

I guess supporting `Base.Order` would also be desirable.

Then methods could be added to these functions for collection types defined in other packages.

Thoughts, especially @Lilith?

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [June 16, 2024, 6:57pm UTC](https://discourse.julialang.org/t/wanted-an-api-for-merging-sorted-collections/115722/2 "2024-06-16T18:57:56Z")

</div>

Seems like it belongs in SortingAlgorithms.jl.

See also: [Add partition algorithms by LilithHafner · Pull Request #82 · JuliaCollections/SortingAlgorithms.jl · GitHub](https://github.com/JuliaCollections/SortingAlgorithms.jl/pull/82) for partitioning.
