# Sorting in a Priority Queue

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.

• This depends on the definition of Queue<K,V>, which should specify the semantics of the data structure. – Yuval Filmus Dec 9 '18 at 1:08
• k(key) V(value) @YuvalFilmus – Pablo Biedma Dec 9 '18 at 14:18

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.

 [(1,item1),(2,item2),(2,item3)]