ソートされていないリンクリストがあります。それを並べ替えるには、コンパレータを指定して値を TreeSet に入れ、それらの値を新しいリンク リストとして返します。それでも、失敗します。
コンパレータ:
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
}
}
ソート機能:
public static LinkedList<Species> sortedAnimals(LinkedList<Species> animals) {
TreeSet<Species> sortedBreeds = new TreeSet<Species>(new SortSpeciesByCommonName());
sortedBreeds.addAll(animals);
return new LinkedList<Species>(sortedBreeds);
}
値をテストすると、すべてが挿入順に表示されます。