Actually it works. It's just that your internal data structure is not updated by Collections.sort()
, and since you base your assertion that the program doesn't work on the output of printList()
, and this relies on that data structure, you see the order of elements untouched. Use this method instead:
public void printParentDataStructure() {
for ( E e : this ) System.out.println( e );
}
and see that your comparator perfectly does its job. So your problem is that you have two data structures and don't keep them in sync. Your next question may be "And how can I keep them sync'ed?" - Well, essentially you should override each and every method, and call super()
like you do in add()
and remove()
. Don't do that! It'd be a complete nonsense.
It's clear that you want to implement a linked list for learning the data strcuture, but maybe you should first better understand the basic principles of OOP programming.