title: Ordering a PriorityQueue by a field of a non-case class
body: <p>I'm currently trying to implement the huffman algorithm using scala. To do this, I'd figured I'd use a PriorityQueue for the ordering of the different Nodes in the tree based on their weight. Thus, I have to create a PriorityQueue of BinarySearchTree Nodes. However, Scala only lets me order by a field of a case class.</p>
  
  <p>This is want I want:</p>
  
  <pre><code>  class BinarySearchTree(weight: Int)
    case class ForkNode(left: BinarySearchTree, right: BinarySearchTree, chars: List[Char], weight: Int) extends BinarySearchTree(weight)
    case class LeafNode(char: Char, weight: Int) extends BinarySearchTree(weight)
  
    def createBST(inputFile: ListMap[Char,Int]): BinarySearchTree = {
      def weightOrder(t2: BinarySearchTree) = t2.weight
      val nodeMap:PriorityQueue[BinarySearchTree] = PriorityQueue(Ordering.by(weightOrder))
      null
    }
  </code></pre>
  
  <p>But it doesn't compile. However, <code>def weightOrder(t2: ForkNode) = t2.weight</code> does compile, but that is not what I want.</p>
  
  <p>How can I order my priority queue based on a field in a non-case class?</p>