Anyone see what I am doing wrong here with `Priori...
# getting-started
a
Anyone see what I am doing wrong here with
PriorityQueue
and
compareBy
?
Copy code
fun testPQ(words: Array<String>) {
    val heap = PriorityQueue<String>(
        compareBy<String>{ it.length }
    )

    words.forEach {
        heap.offer(it)
    }

    println(heap)
}

fun main() {
    val input = arrayOf("the","day","is","sunny","tha","a","def","sunny","as","by")
    testPQ(input)
}
The result I see is
[a, as, is, day, by, the, def, sunny, sunny, tha]
by
and
tha
seems to be out of order.
e
the head of a priority queue is the least element, but no guarantees are made about the iteration order of the rest of the pq. you can only determine the second-least by removing the first element
👍 1
your choice of "heap" as the variable name there is telling - look up how a binary heap is implemented
a
Of course. What was I thinking. Thanks for pointing that out. I’m not sure why I thought that it should be sorted.