title: How to pass a custom comparator function for BinaryHeap in rust?
body: <p>Say I have the following struct:</p>
  <pre class="lang-rust prettyprint-override"><code>struct Node {
      location: Location,
      price: f32
  }
  </code></pre>
  <p>And now, I want to store a number of <code>Node</code>s in a BinaryHeap.</p>
  <pre class="lang-rust prettyprint-override"><code>let heap: BinaryHeap&lt;Node&gt; = BinaryHeap::new(); 
  </code></pre>
  <p>However, since I have not implemented <code>Ord</code> for <code>Node</code>, the above declaration would err.</p>
  <h2>Problem</h2>
  <p>There are at least two ways to implement the <code>cmp</code> function for <code>Node</code>:</p>
  <pre class="lang-rust prettyprint-override"><code>// compare by price
  fn cmp(&amp;self, other: &amp;Self) -&gt; Ordering {
          other.price.cmp(&amp;self.price)
  }
  
  // or compare by distance (not sure if I can add an extra argument, but let's assume we can)
  fn cmp(&amp;self, other: &amp;Self, source_node: &amp;Node) -&gt; Ordering {
          other.distance_to(source_node).cmp(&amp;self.distance_to(source_node))
  }
  </code></pre>
  <p>And I want to give user the ability to pass in their own custom <code>cmp</code> function, and use that for BinaryHeap ordering.</p>
  <p>In Java, you can pass in a custom comparator function for PriorityQueue: see <a href="https://stackoverflow.com/a/2555325/4784433">here</a> for an example. But it seems like BinaryHeap in rust doesn't really have that ability.</p>
  <p>Are there any workarounds to achieve this?</p>