title: Collections.sort() is giving me compilation error. How to do sort() on ArrayList<T>, where T is coming at runtime (dynamic)?
body: <p>I am practicing ArrayList and I wanted to create a method that sorts the List&lt;&gt; of any type T (T can be Integer, String, or maybe something else).</p>
  <p>I am aware that sort works on those classes which have implemented Comparable interface.</p>
  <p><strong>Expected: sort should work if the class T (dynamically coming in the parameter) is implementing Comparable.</strong></p>
  <p><strong>Actual:</strong> I am stuck here as the sort is giving me compilation error :</p>
  <p><em>The method sort(List&lt;T extends Comparable&lt;? super T&gt;&gt;) in the type Collections is not applicable for the arguments (List)</em></p>
  <p>I think this is because sort expected the type of List to be predefined (like Integer or String) but I am giving it T which is undefined until the program runs.</p>
  <p>This is where my main() is (focus in last line):</p>
  <pre><code>    public static void main(String[] args) {
          arrayListFunctions = new ArrayListFunctions();
          
          List&lt;Integer&gt; al = new ArrayList&lt;Integer&gt;();
  
          //Imagine adding some integers...
  
          //This line is where it begins
          arrayListFunctions.sortList(al);
      }
  </code></pre>
  <p>This is where <strong>arrayListFunctions.sortList(al);</strong> points to:</p>
  <pre><code>    &lt;T&gt; List&lt;T&gt; sortList(List&lt;T&gt; input) {
          if (input instanceof Comparable) {
              input = Collections.sort(input);
              System.out.println(&quot;List is sorted.&quot;);
              printUsingIterator(input);
          }
          else
              System.out.println(&quot;Failed to sort as type of list is not comparable.&quot;);
              
          return input;
      }
  </code></pre>