# How to get the key with the maximum priority in a priority queue using Julia

**URL:** <https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764>\
**Category:** General Usage\
**Created:** [May 30, 2019, 12:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764 "2019-05-30T12:01:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 30, 2019, 12:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/1 "2019-05-30T12:01:41Z")

</div>

I checked the following link [https://juliacollections.github.io/DataStructures.jl/latest/priority-queue.html](https://juliacollections.github.io/DataStructures.jl/latest/priority-queue.html) and did not find any related information. I guess one can use some trick with `Base.Order.ForwardOrdering` to accomplish this. Could anyone please let me know about your opinion of this?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 30, 2019, 12:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/2 "2019-05-30T12:17:09Z")

</div>

dequeue! or dequeue\_pair!

Or peek?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 30, 2019, 4:16pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/3 "2019-05-30T16:16:28Z")

</div>

Thank you for your reply. However, if I understand it correctly, dequeue! will remove and return the key priority pair with the smallest priority from the priority queue.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 30, 2019, 4:24pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/4 "2019-05-30T16:24:52Z")

</div>

Ah, so you have a priority queue ordered by smallest value, but you want to extract the largest value?  
Then you will have to do a complete search. Or maintain two priority queues that somehow point to the same data, ordered

by smallest and largest value?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 30, 2019, 4:31pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/5 "2019-05-30T16:31:40Z")

</div>

thank you. I have already found a way to get around this. 😉

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 30, 2019, 4:44pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/6 "2019-05-30T16:44:39Z")

</div>

Maybe you could share with us how you solved the problem?

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 30, 2019, 5:13pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/7 "2019-05-30T17:13:12Z")

</div>

I did not solve it directly and explicitly. If so, I would post the solution already. It’s more like a “get around” and a design thing. I would guess, that if the priorities are numbers, then add a negative sign to all the priorities and then create another priority queue would solve this.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 30, 2019, 5:25pm UTC](https://discourse.julialang.org/t/how-to-get-the-key-with-the-maximum-priority-in-a-priority-queue-using-julia/24764/8 "2019-05-30T17:25:16Z")

</div>

You can specify the sorting method when defining the priority queue.
