My very first post question here, be gentle please :)
I am trying to create a comparator in Java that sorts based on various criteria. The Main class is very basic, it has several variables which the sorting is based on.
For example, a "Book" class, which has :
- page number,
- title,
- average wordcount per page.
Here is what "intructions" i was given:
Write a ComplexComperator, which can sort based on multiple variables. Make 2 constructors:
One with 2 parameters
public ComplexComparator(Comparator<Book> x, Comparator<Book> y)
If both items are equal based on first parameter, then it sorts based on the second parameter,
And another with 4 parameters
public ComplexComparator(Comparator<Book> x, boolean h, Comparator<Book> y, boolean i)
If the logical variables are true, it sorts it in a natural order based on that Comparator - so it works the same as the 2-parameter constructor. If one of the logical variables takes a false value, it will reverse the natural order.
So, thats it. I wrote simple Comparators but i don't know how to handle this one. I don't know what the compare method should look like; or even if I have to add some kind of class variable... Thank you for any help!
ComplexComparatorclass ? and first constructor codeComparator.thenComparing(...)? It sounds like "If 2 data is equal based on first parameter, then it sorts based on the second parameter". So basically aComplexComparatorwould wrap aComparatorchain plus setting the sort order?