So I was trying to sort words alphabetically using linear sort and delete the word if there is already a word like it. I used the following method:
import java.util.Arrays;
public class Sorting {
public static void main(String[] args) {
String[] array = new String[] { "pepperoni", "ham", "bacon",
"pineapple", "ham", "sausage", "onion", "bacon" };
System.out.println("Before sorting: " + Arrays.toString(array));
for (int i = 0; i < array.length; i++) {
int min = i;
for (int j = i; j < array.length; j++) {
if (array[min].compareTo(array[j]) > 0) {
min = j;
}
else if (array[min].equals(array[j]) == true) {
array[j] = "";
}
}
String tmp = array[i];
array[i] = array[min];
array[min] = tmp;
}
System.out.println("After sorting: " + Arrays.toString(array));
}
}
But everything is deleted. Without the else if
statement it will be sorted out, but with it everything is deleted.
Before sorting: [pepperoni, ham, bacon, pineapple, ham, sausage, onion, bacon]
After sorting: [, , , , , , , ]
Can someone point out what's wrong with this code?