title: SelectionSort implemented with Generic Method - Wrong Results
body: <p>I'm working through Deitel &amp; Deitel's "Java - How To Program" and I'm stumped on why this implementation I came up with of a generic selection sort method is not working. I'm sure I must be missing some small detail, but after researching the API and a few resources on Generics, I'm coming up cold. While the program runs and performs some kind of sort, it definitely does NOT come out sorted in numerical order! I can't tell if I'm misunderstanding Generics, or just the selection sort algorithm. Any help would be appreciated!</p>
  
  <p>The output I receive on running the selection sort on the intArray is: 
  0, 1, -23, 7, 54 </p>
  
  <p>The output for the floatArray after the sort is: 
  -1.1, -10.3, 0.4, 4.5</p>
  
  <p><strong><em>UPDATE</em></strong> I just tried this without using negative values and it sorts fine, what is that about???</p>
  
  <p>Here is the complete Sorter class that performs the selection sort:</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.ArrayList;
  
  public class Sorter {
      public static void main(String[] args) {
          Integer[] intArray = {1, 7, -23, 54, 0};
          Float[] floatArray = {0.4f, -10.3f, 4.5f, -1.1f};
  
          ArrayList&lt;Integer&gt; intList = new ArrayList&lt;&gt;(Arrays.asList(intArray));
          ArrayList&lt;Float&gt; floatList = new ArrayList&lt;&gt;(Arrays.asList(floatArray));
  
          System.out.printf("Lists before selectionSort: %n%s%n%s%n%n",
              intList, floatList);
  
          selectionSort(intList);
          selectionSort(floatList);
  
          System.out.printf("Lists after selectionSort: %n%s%n%s%n%n",
              intList, floatList);
      }
  
      public static &lt;T extends Comparable&lt;T&gt;&gt; void selectionSort(ArrayList&lt;T&gt; list) {
          // helps determine whether or not a swap will occur
          boolean needsSorting = false;
          // keeps track of the index of the smallest value
          int smallest = 0;
  
          // outer for walks the portion of the list that will be swapped
          for (int i = 0; i &lt; list.size() - 1; i++) {
              // inner for searches for a smaller value than the front of list
              for (int j = i + 1; j &lt; list.size(); j++) {
                  // if the inner value is less than the outer value
                  if (list.get(j).compareTo(list.get(i)) &lt; 0) {
                      // store the index of the smaller value
                      smallest = j;
                      // set the boolean flag to true so the sort will happen
                      needsSorting = true;
                  }
              }
  
              // if the list needs sorting
              if (needsSorting) {
                  // get the value of the outer loop, store in generic variable
                  T temp = list.get(i);
                  // replace value of outer loop with value at the smallest index
                  list.set(i, list.get(smallest));
                  // replace value at what was smallest index with the value that
                  // was at the index of the outer loop
                  list.set(smallest, temp);
  
                  needsSorting = false;
              }
          }
      }
  }
  </code></pre>