title: How can I convert this sorting method to be generic in Java?
body: <p>This question is kind of long... so bear with me please.</p>
  
  <p>I have to convert a SelectionSort method that was covered in my book that was built to sort arrays, and make it generic so that I can enter either doubles or ints into it and have it work.</p>
  
  <p>It doesn't seem to allow generic arrays, so I'm attempting to use an ArrayList. The problem here is since the int and doubles are now in Integer and Double wrappers, it breaks the SelectionSort method.</p>
  
  <p>I've attempted to fix it, but I'm having no luck. I'll post the original SelectionSort method below, and then the class and driver that I'm creating.</p>
  
  <p>Original SelectionSort:</p>
  
  <pre><code>public class SelectionSort {
  private int[] data;
  private static final Random generator = new Random();
  
  public SelectionSort(int size) {
      data = new int[size];
  
      for(int i = 0; i &lt; size; i++) {
          data[i] = 10 + generator.nextInt(90);
      }
  }
  
  public void sort() {
      int smallest;
  
      for(int i = 0; i &lt; data.length - 1; i++) {
          smallest = i;
  
          for(int index = i + 1; index &lt; data.length; index++) {
              if(data[index] &lt; data[smallest]) {
                  smallest = index;
              }
          }
  
          swap(i, smallest);
      }
  }
  
  public void swap(int first, int second) {
      int temporary = data[first];
      data[first] = data[second];
      data[second] = temporary;
  }
  
  }
  </code></pre>
  
  <p>My simple driver program:</p>
  
  <pre><code>public class GenericsDriver {
  
  public static void main(String[] args) {
  
      SelectionSort&lt;Integer&gt; intSort = new SelectionSort&lt;Integer&gt;();
  
      intSort.AddGrade(100);
      intSort.AddGrade(90);
      intSort.AddGrade(50);
      intSort.AddGrade(80);
      intSort.AddGrade(95);
  
      intSort.PrintNumbers();
  
      //sort here
  
      intSort.PrintNumbers();
  
      SelectionSort&lt;Double&gt; doubleSort = new SelectionSort&lt;Double&gt;();
  
      doubleSort.AddGrade(100.1);
      doubleSort.AddGrade(90.4);
      doubleSort.AddGrade(50.7);
      doubleSort.AddGrade(100.2);
      doubleSort.AddGrade(100.5);
  
      doubleSort.PrintNumbers();
  
      //sort here
  
      doubleSort.PrintNumbers();
  
  }
  
  }
  </code></pre>
  
  <p>The new class and my attempt to repurpose the SelectionSort method:</p>
  
  <pre><code>import java.util.*;
  
  public class SelectionSort &lt;T&gt; {
  
  private Array&lt;T&gt; numbers;
  
  public SelectionSort() {
      numbers = new ArrayList&lt;T&gt;();
  }
  
  public void AddGrade(T number) {
      numbers.add(number);
  }
  
  public void PrintNumbers() {
      System.out.println(numbers.toString());
  
  }
  
  public &lt;T extends Comparable&lt;T&gt;&gt; selectionSort() {
      int smallest;
  
      for(int  i = 0; i &lt; numbers.size(); i++) {
          smallest = i;
  
          for(int index = i + 1; index &lt; numbers.size(); index++) {
              if(numbers.) {
              //I've tried everything here...
                              //from using number.get(index), and everything else
                              //I could think of
              }
          }
      }
  }
  
  public void swap(int first, int second) {
  
  }
  
  }
  </code></pre>
  
  <p>As you can see... I haven't had any luck with sort or swap within my new class. I can't get it to work. My instructions have a hint that I should use > in my sort method...but nothing is giving me the ability to use a .compareTo() method.</p>
  
  <p>This is the actual directive from my book:
  Write a generic method selectionSort based on the sort program of Fig 19.6 and 19.7 (That's the code I gave above). Write a test program that inputs, sorts and outputs an Integer array and a Float array. Hint: Use > in the type-parameter section for method selectionSort, so that you can use method compareTo() to compare the objects of the type that T represents.</p>
  
  <p>Could someone please give me some guidance here? Thanks.</p>