LinkedList
is not a sorted collection, so you cannot pass it a Comparator during construction. Observe that none of its constructors take a Comparator
.
Instead, you would build it and add to it, and then use
Collections.sort(myList, new MyComparator());
You can make a write a Comparator
of a parameterized type T
with the following syntax.
public class MyComparator implements Comparator<T>() {
public int compare (T a, T b) {
// Put comparison logic here.
}
}
Obviously, you want to replace T
above with the name of your type.