2

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!

4
  • 1
    Can you share the ComplexComparator class ? and first constructor code
    – azro
    Jul 24, 2018 at 19:01
  • I don't get it. Is there a need for a complex comparator since comparators can be chained by Comparator.thenComparing(...)? It sounds like "If 2 data is equal based on first parameter, then it sorts based on the second parameter". So basically a ComplexComparatorwould wrap a Comparatorchain plus setting the sort order?
    – LuCio
    Jul 24, 2018 at 19:08
  • I don't really have anything to share. I don't really know how the class should look like. The "public class ComplexComparator implements Comparator<Book>" which i have to write given with the instructions above.
    – Zalajin
    Jul 24, 2018 at 19:09
  • @LuCio It's not unlikely that this is an assignment from pre-Java-8-times. Regardless of that: Implementing such a comparator can create some familiarity with the standard API and related classes. There still are some degrees of freedom for the implementation, in terms of genericity, but the exact requirements did IMHO unnecessarily constrain that... :-/
    – Marco13
    Jul 24, 2018 at 22:05

2 Answers 2

5

1. How to do

You may add a Comparator<Book> in your ComplexComparator class and you just need to check the different possibilities for the conditions in you constructor :

public class ComplexComparator implements Comparator<Book>{

    private Comparator<Book> comp;

    public ComplexComparator(Comparator<Book> x, Comparator<Book> y) {
        comp = x.thenComparing(y);
    }

    public ComplexComparator(Comparator<Book> x, boolean h, Comparator<Book> y, boolean i) {
        if (h && i) {
            comp = x.thenComparing(y);
        } else if (h) {
            comp = x.thenComparing(y.reversed());
        } else if (i) {
            comp = x.reversed().thenComparing(y);
        } else {
            comp = x.reversed().thenComparing(y.reversed());
        }
    }

    @Override
    public int compare(Book o1, Book o2) {
        return comp.compare(o1, o2);
    }
}

2. How to create it

If you have the correct getters you can use as

public static void main(String[] args) {
    Comparator<Book> page = Comparator.comparing(Book::getPageNb);
    Comparator<Book> title = Comparator.comparing(Book::getTitle);

    ComplexComparator c1 = new ComplexComparator(page, title);
    ComplexComparator c2 = new ComplexComparator(page, true, title, false);
}

3. How to use it

Full Workable Demo

1
  • Thank you very very much! I might have overthought it a little, looks very logical. Thank you again!
    – Zalajin
    Jul 24, 2018 at 19:25
2
public static void main(String[] args) {
    Comparator<Book> page = Comparator.comparing(Book::getPageNb);
    Comparator<Book> title = Comparator.comparing(Book::getTitle);

    // you dont need a combining "complex" class - 
    // just compose using .thenCompare() and/or 
    // .reversed() as required e.g.:

    Comparator complex1 = page.thenComparing(title);
    Comparator complex2 = page.reversed().thenComparing(title);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.