title: Generics and sorting in Java
body: <p>Suppose you write a static function in Java to sort an array, much like <code>Arrays.sort()</code>. The problem with <code>Arrays.sort()</code> is that it receives an array of Object, and throws a <code>ClassCastException</code> if its elements don't implement <code>Comparable</code>.</p>
  
  <p>So you want your function to receive as an argument an array of a subtype of <code>Comparable</code>. Something like that could work:</p>
  
  <pre><code>static &lt;T extends Comparable&gt; void sort(T[] array);
  </code></pre>
  
  <p>The problem with that signature is that you can still pass an array of Comparables with Integers and Strings for instance, which would cause a <code>RuntimeException</code>.</p>
  
  <p>So, how can you create a function that will receive only an array whose elements implement Comparable and have all the same type (e.g. Integer, String, etc?)</p>