So I have the following method
public static <T extends Comparable<? super T>> void bubbleSort( T[] a)
//public static void bubbleSort(Comparable[] a)
{
for(int top = a.length-1; top > 0; top--)
for(int i = 0; i < top; i++)
if(a[i+1].compareTo(a[i]) < 0)
{ T tmp = a[i];
//Comparabl tmp = a[i];
a[i] = a[i+1];
a[i+1] = tmp;
}
}
How do I change the method signature in order to be able to call it from something like
public int sortByTitle()
{
return Sorting.bubbleSort(lib); // (lib is lib = new ArrayList<Object>();
}
I must not use collection methods or comparator object.
You are making use of the
.compareTomethod which is not implemented by theObjectclass which in turn would not make your Bubble Sort work since there will be no implementation of the.compareTomethod.In your case, I think it would be best to create an array list of items which actually implement the comparable interface, which is the opposite of what you are trying to do (change the method signature to fit the method call).
Once that you will have done that, you should be able to call the method by converting the list to an array using toArray method.
Another thing worth noting is that your
bubbleSortmethod does not return anything, thus your method call should not compile.