Right now I'm working on an assignment and we are give a list of words to be sorted alphabetically, and we have to use the compareTo method. I need it to sort the entire list so it can then be printed. I've been struggling with this final part for about an hour now, can somebody lend an insight into why this code isn't working at all?
public static void selectionSort(final Comparable[] array)
{
if (array == null)
{
throw new NullPointerException("The array is null.");
}
for(int i = 0; i < (array.length - 1); i++)
{
Comparable thisWord = array[i];
int num = i;
while(thisWord.compareTo(array[num]) > 0)
{
Comparable tmp = array[num];
array[num] = thisWord;
array[i] = tmp;
num++;
}
}
}
I might be way off, either way I don't feel like I'm making any progress. Any help would be much appreciated!!