# Keep n largest values compared by f

**URL:** <https://discourse.julialang.org/t/keep-n-largest-values-compared-by-f/137699>\
**Category:** General Usage\
**Tags:** question, data\_structures\
**Created:** [June 19, 2026, 1:42pm UTC](https://discourse.julialang.org/t/keep-n-largest-values-compared-by-f/137699 "2026-06-19T13:42:31Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 19, 2026, 2:59pm UTC](https://discourse.julialang.org/t/keep-n-largest-values-compared-by-f/137699/5 "2026-06-19T14:59:54Z")

</div>

> [@foobar\_lv2](#):
>
> You don’t need a min-max heap, you just need the minimal value (if the new value is larger than the current min, and you have 1000 values, then pop the old min and push the value).

As I commented in the other thread, you can (in principle) do better than this by combining the pop and push operations: [Maintaining a fixed size "top N" values list - #7 by stevengj](https://discourse.julialang.org/t/maintaining-a-fixed-size-top-n-values-list/78868/7) (and that implementation also accepts an arbitrary `Ordering`).

---

_[View the full topic](https://discourse.julialang.org/t/keep-n-largest-values-compared-by-f/137699)._
