# Add BinaryHeap::into\_iter\_sorted()

**URL:** <https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749>\
**Category:** libs\
**Created:** [April 1, 2019, 4:37am UTC](https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749 "2019-04-01T04:37:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ordovicia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ordovicia/32/3910_2.png) [@ordovicia](https://internals.rust-lang.org/u/ordovicia)\
**Post date:** [April 1, 2019, 4:37am UTC](https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749/1 "2019-04-01T04:37:12Z")

</div>

Existing `BinaryHeap::iter()` returns an iterator that visits underlying data in arbitrary order. I think it’s worth adding a sorted version. `BinaryHeap` already has `into_sorted_vec()` method, but using the iterator should be more performant in some cases.

I couldn’t find any previous discussion.

One question is that in what order the sorted iterator should visit the underlying data. `BinaryHeap::pop()` returns the greatest item, while `into_sorted_vec()` sorts the data in ascending order.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [April 1, 2019, 4:55am UTC](https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749/2 "2019-04-01T04:55:04Z")

</div>

Given that one can now just do `std::iter::from_fn(move || my_heap.pop())`, I’m less confident that this is worth it in `std`, since the api discussions are non-obvious.

---

<div class="post-metadata">

**Author:** ![ordovicia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ordovicia/32/3910_2.png) [@ordovicia](https://internals.rust-lang.org/u/ordovicia)\
**Post date:** [April 1, 2019, 8:17am UTC](https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749/3 "2019-04-01T08:17:23Z")

</div>

Ah, I didn’t come up with using `from_fn()`. That’s a concise and good way. Thanks!

And I just found a discussion about the sorted version of the iterator. Sorry.

> <https://github.com/rust-lang/rust/issues/59278>

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 30, 2019, 8:28am UTC](https://internals.rust-lang.org/t/add-binaryheap-into-iter-sorted/9749/4 "2019-06-30T08:28:47Z")

</div>

Closed on June 30, 2019, 8:28am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
