I have an unsorted linked list. To sort it, I thought I'd put the values into a TreeSet with a comparator supplied, then return those values as a new linked list. Yet, it fails.
Comparator:
public class SortSpeciesByCommonName implements Comparator<Species> {
/**
* a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
*/
@Override
public int compare(Species arg0, Species arg1) {
return arg0.getName().compareTo(arg1.getName()); //arg.getName() is String
}
}
Sorting function:
public static LinkedList<Species> sortedAnimals(LinkedList<Species> animals) {
TreeSet<Species> sortedBreeds = new TreeSet<Species>(new SortSpeciesByCommonName());
sortedBreeds.addAll(animals);
return new LinkedList<Species>(sortedBreeds);
}
When testing the values, everything appears to still be in insertion order.