title: Why does this Guava Ordering of a Map by value work?
body: <p>I'm new to the Guava library and trying to use some of its classes to simplify my code.  I've run across the need to sort a Map by value.  A quick search found <a href="https://stackoverflow.com/a/8897384/827480">this post</a> which posts an accepted answer as the following code snippet:</p>
  
  <pre><code>Ordering&lt;Map.Entry&lt;Key, Value&gt;&gt; entryOrdering = Ordering.from(valueComparator)
    .onResultOf(new Function&lt;Entry&lt;Key, Value&gt;, Value&gt;() {
      public Value apply(Entry&lt;Key, Value&gt; entry) {
        return entry.getValue();
      }
    }).reverse();
  // Desired entries in desired order.  Put them in an ImmutableMap in this order.
  ImmutableMap.Builder&lt;Key, Value&gt; builder = ImmutableMap.builder();
  for (Entry&lt;Key, Value&gt; entry : 
      entryOrdering.sortedCopy(map.entrySet())) {
    builder.put(entry.getKey(), entry.getValue());
  }
  return builder.build();
  // ImmutableMap iterates over the entries in the desired order
  </code></pre>
  
  <p>Can someone please clarify for me how this works?  I am failing to understand something.</p>
  
  <p>The definition of Ordering is <code>Ordering&lt;T&gt;</code>.  In this case <code>&lt;T&gt;</code> would be <code>Map.Entry</code>.  Furthermore, the method signature of <code>onResultOf</code> is <code>onResultOf(Function&lt;F,? extends T&gt; function)</code>.  </p>
  
  <p>In the above code, <code>onResultOf</code> is being called with the following parameter:</p>
  
  <pre><code>onResultOf(new Function&lt;Entry&lt;Key, Value&gt;, Value&gt;())
  </code></pre>
  
  <p>Which in this case would imply that:</p>
  
  <pre><code>&lt;F&gt; = Entry&lt;Key, Value&gt;
  &lt;? extends T&gt; = Value
  </code></pre>
  
  <p>Which in turn implies that <code>Value</code> is a type that would extend a <code>Map.Entry</code>.</p>
  
  <p>How is that possible?  How can <code>Value</code> be a type that would extend <code>Map.Entry</code>, when the entry map contains Key, Value?</p>
  
  <p>I am sure I am misreading or misunderstanding something, but if anyone can shed some light on this, I am hoping it will help me understand Guava and the Ordering() class a little better.  Especially how the <code>onResultOf(Function)</code> works.</p>