title: Crafting a Comparator object for sorting generic List<? extends T> using Collections.sort()
body: <p>I am trying to implement a generic sort utility method for a List of objects of any class that implements MyInterface. Per Java API (<a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html" rel="nofollow noreferrer">http://java.sun.com/javase/6/docs/api/java/util/Collections.html</a>), Collections.sort() method signature is:</p>
  
  <pre><code>public static &lt;T&gt; void sort(List&lt;T&gt; list, Comparator&lt;? super T&gt; c)
  </code></pre>
  
  <p>I am not sure if List with a wildcard parameter can substitute a "plain" parametrized List, but I tried:</p>
  
  <pre><code>static void mySort(List&lt;? extends MyInterface&gt; myList, SortCriteria mySortCriteria) {
      Collections.sort(myList, new Comparator&lt;? super MyInterface&gt;() {
          ...
      });
  }
  </code></pre>
  
  <p>and got a compile time error,</p>
  
  <pre><code>The type new Comparator(){} cannot extend or implement Comparator&lt;? super MyInterface&gt;
  A supertype may not specify any wildcard.
  </code></pre>
  
  <p>So, I changed it to:</p>
  
  <pre><code>static void mySort(List&lt;? extends MyInterface&gt; myList, SortCriteria mySortCriteria) {
      Collections.sort(myList, new Comparator&lt;MyInterface&gt;() {
          ...
      });
  }
  </code></pre>
  
  <p>And it compiles and works. Any good explanations?</p>