My computer science teacher gave me a challenge to create a function that can take in an array of any object and sort it using bubble sort. Since the Object class is the parent class of all classes, I thought of taking in an array of Objects but I can't really sort it since the Object class doesn't have a comparable. I'm kind of stuck right now because I don't know what to pass in to the function. My teacher said that I should pass in a Comparable interface but I'm afraid I don't know enough to know what he means by that.
This is what I tried but I know it doesn't work:
public static void bubble(Object arr[]) {
for (int i = 0; i < arr.length - 1; i ++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j].compareTo(arr[j + 1]) > 0) {
Object temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
I don't know much about Java so any help is greatly appreciated, thank you!