I have a program that allows user to delete an element from an array and I am trying to sort them in alphabetic order using compareTo(); through a for loop. However, the null values are giving me problems. For example an array with null values:
String[] myArray = {"Apple", "Banana", null, "Durian", null, null, "Grapes"};
When Java is comparing them and reads a null value, it would give me a NullPointerException.
Is there any way that I can sort this array with null values at the back? For example:
{"Apple", "Banana", "Durian", "Grapes", null, null, null}
I know that using Vectors can solve the problem but I am just curious if there is any way that I can just do it without changing my array to vectors.
Arrays.sort(array, Ordering.natural().nullsLast())