title: implementing comprable method in a generic way in Java for sorting
body: <p>I am trying to implement a generic selection sort which can take any objects and do the sorting on it. I can promise to the compiler that whatever objects I am comparing, has the compareTo method implemented for it. But I get compile error for the following code</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  
  public class SelectionSortGenerics implements Comparable&lt;E&gt; {
  
      private &lt;E&gt; void swap(E[] a, int i, int j) {
          if (i != j) {
              E temp = a[i];
              a[i] = a[j];
              a[j] = temp;
          }
      }
  
      public &lt;E&gt; void selectionSort(E[] a) {
          for (int i = 0; i &lt; a.length - 1; i++) {
              // find index of smallest element
              int smallest = i;
              for (int j = i + 1; j &lt; a.length; j++) {
                  if (a[j].compareTo(a[smallest])&lt;=0) {
                      smallest = j;
                  }
              }
  
              swap(a, i, smallest);  // swap smallest to front
          }
      }
  
      public static void main(String[] args){
          SelectionSortGenerics firstsort = new SelectionSortGenerics();
  
          Integer[] arr = {3,4,1,5};
          System.out.println("before sorting int: "+ Arrays.toString(arr));
          firstsort.selectionSort(arr);
          System.out.println("After sorting int : "+Arrays.toString(arr));
           String[] arr1= {"acd","ded","dal","bad","cle"};
           System.out.println("before sorting String: "+ Arrays.toString(arr1));
           firstsort.selectionSort(arr1);
           System.out.println("After sorting String : "+Arrays.toString(arr1));
           Character[] arr2= {'c','e','a','d','c'};
           System.out.println("before sorting char: "+ Arrays.toString(arr2));
           firstsort.selectionSort(arr2);
           System.out.println("After sorting char : "+Arrays.toString(arr2));
      }
  }
  </code></pre>
  
  <p>As you can see, the objects I am passing in main method are Integer, String and Character, which has compareTo methods. how would make the above code work. Anywhere, casting is needed?
  Thanks for your help.</p>