# How to return k largest elements of a vector in descending order?

**URL:** <https://discourse.julialang.org/t/how-to-return-k-largest-elements-of-a-vector-in-descending-order/89559>\
**Category:** New to Julia\
**Tags:** sort, maxima\
**Created:** [October 31, 2022, 12:31pm UTC](https://discourse.julialang.org/t/how-to-return-k-largest-elements-of-a-vector-in-descending-order/89559 "2022-10-31T12:31:41Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 31, 2022, 7:37pm UTC](https://discourse.julialang.org/t/how-to-return-k-largest-elements-of-a-vector-in-descending-order/89559/6 "2022-10-31T19:37:46Z")

</div>

by @stevengj

> the DataStructures.jl package [already contains such a function](https://juliacollections.github.io/DataStructures.jl/latest/heaps/#Functions-using-heaps-1), implemented using heaps.
> 
> Just call `nlargest(n, array)` from DataStructures.jl

from [this](https://discourse.julialang.org/t/the-quasi-best-maxn-function/52813/6) discussion

---

_[View the full topic](https://discourse.julialang.org/t/how-to-return-k-largest-elements-of-a-vector-in-descending-order/89559)._
