Does a priority queue Queue<K,V> always sort its elements based on the value of each element or its key?

I know than the priority in the queue is based on the key, and that the element with the highest priority has the minimal key (Correct me if I am wrong) But I am not sure about wether the pq sorts based on value.

  • 1
    $\begingroup$ This depends on the definition of Queue<K,V>, which should specify the semantics of the data structure. $\endgroup$ Commented Dec 9, 2018 at 1:08
  • $\begingroup$ k(key) V(value) @YuvalFilmus $\endgroup$
    – user97496
    Commented Dec 9, 2018 at 14:18

2 Answers 2


The definition of a priority queue is that elements will be dequeued in order of priority. The definition says nothing about the order of items of equal priority so an implementer can do anything, within reason, and still be justified in calling the resulting structure a priority queue.


Think about something like this:


    add (10,item4):



    add (1,item5)


The value would have nothing to do , the elements will be stored depending on its queue. (that was my final reasoning)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.