How do I amend my code for bubble sorting integers so I can re-use it for strings too? Or do I need to create a completely new class for sorting strings exclusively. Thanks!
MAIN CLASS:
public class BubbleSortTest {
public static void main(String[] args) {
Integer[] integers = {25, 15, 45, 5, 40, 50, 10, 20, 35, 30};
ArrayUtility.display(integers);
BubbleSort.sort(integers);
ArrayUtility.display(integers);
String[] strings = {"def", "efg", "bcd", "abc", "fgh", "cde", null};
ArrayUtility.display(strings);
BubbleSort.sort(strings);
ArrayUtility.display(strings);
}
}
SORT CLASS:
public class BubbleSort {
public static void sort(Integer[] numbers) {
Integer temp;
for (Integer i = 0; i < numbers.length; i++) {
for (Integer j = 1; j < (numbers.length) - i; j++) {
if (numbers[j - 1] > numbers[j]) {
//SWAPPING ELEMENTS
temp = numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = temp;
}
}
}
}
}
TreeSet. (Generics + Comparator)