Today I was asked this interview question:
If I have a
Personclass withname,ageandsalaryfields, and I put 100 new instances of thisPersonin anArrayList, and then doCollections.sort(list), then on what parameter will the list be sorted?
I understand that I need to have the Person class implement Comparable and then override compareTo, but if I don't do that, what will happen?
It wouldn't compile: the 1-argument version of Collections.sort expects a list of Comparables. Specifically, a List<T> where T implements Comparable<? super T>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With