It compiles but gives run time ClassCastException error "Student can not be casted to Comparable". I am not using Comparable. I am using Comparator.
Why do I get that error ? what's the best and easy way to fix assuming I still use Comparator and Arraylist ?
If you want to make Students comparable, you should implement the Comparable interface instead. If you insist on making the Student class to be a Comparator as well, you have to use the sort method the following way :
This will tell the Collections class to use a new comparator to compare the Students. That looks awkward though. You should use Comparable instead.