Im trying to change the sort method in the following code to sort objects of any type rather than just string but a bit stumped, all advice welcome.
class SortNames {
static void sort(String[] s) {
for (int i = 0; i<s.length; i++) {
for (int j = i+1; j < s.length; j++) {
if (s[i].compareTo(s[j])>0) {
String t;
t = s[i]; s[i] = s[j]; s[j] = t;
}
}
}
}
public static void main(String[] args) {
String[] names = {"Pete","Jill","May","Anne","Tim"};
sort(names);
for (int i=0; i<names.length; i++)
System.out.println(names[i]);
}
}
Turn
SortNamesintoSortNames<T extends Comparable<? super T>>, and changeString[]toT[], which is howCollections.sort()work: