Question: How can i use Comparator while using an Iterator (Java) I have a class called book that has a method that returns a comparator public

How can i use Comparator while using an Iterator (Java)

I have a class called book that has a method that returns a comparator

public Comparator sortByYear() { return new Comparator() { @Override public int compare(Book o1, Book o2) { if(o1.getDate().getYear() > o2.getDate().getYear()) return 1; else if(o1.getDate().getYear() < o2.getDate().getYear()) return -1; else return 0; } }; }

in my driver, im trying to iterate through my list, but I need to use the comparator to sort them. How can I do this without using Collections.sort() or Arrays.sort()?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!