1

From all of the sort method examples i've seen, i have never come across one where they pass in a definition of a Comparator. If the comparator is designed to return a negative, positive or 0 to sort the objects, what does the sort method do with this information? How does the sort method obtain meaningful information from the Comparator definition and perform its operation?

Here is the sort method in question:

    public void sort() {
        library.sort(new BookComparator());
    }

Here is the entire Comparator class:

import java.util.Comparator;

public class BookComparator implements Comparator<Book> {
    public int compare(Book a, Book b) {
        if(a == null || b == null) {
            throw new NullPointerException();
        }

        int result = 0 ;

        if(a.getAuthor() == null) {
            if(b.getAuthor() != null) {
                result = -1;
            }
        } else if(b.getAuthor() == null) {
            result = 1;
        } else {
            result = a.getAuthor().compareTo(b.getAuthor());
        }
        if(result !=0) {
            return result;
        }

        if(a.getTitle() == null) {
            if(b.getTitle() != null) {
                result = -1;
            }
        } else if(b.getTitle() == null) {
            result = 1;
        } else {
            result = a.getTitle().compareTo(b.getTitle());
        }

        if(result !=0) {
            return result;
        }

        if(a.getYear() < b.getYear()){
            return -1;
        } else if (a.getYear() == b.getYear()){
            return 0;
        } else {
            return 1;
        }
    }
}
2
  • 3
    All sorts, in all languages, require some kind of comparator. Java’s default comparator is to use what’s called the “natural ordering”, which is the comparator of Comparable instances. For non-Comparables (or to override the default) you provide the comparator to use (instead)
    – Bohemian
    Oct 12, 2019 at 23:49
  • Can you explain a little bit what your precise question is? Because really, the answer to your question is basically "the sorting algorithm uses the comparator to compare". But I am not sure if that answer will be satisfactory to you. Oct 12, 2019 at 23:51

2 Answers 2

4

You can pass an instance of a Comparator into a sort method so you can sort on different aspects other than the default/natural way of sorting the object.

For example if have a Book object the natural sorting might be based on the Title. But what if you wanted to sort based on the Dewey Decimal number? Or based on the Author's name? Or the number of pages? You could do that by writing a Comparator that compares those fields of the Book object.

The actual sort algorithm does not need to know the object being sorted. It just needs a Comparator that is consistent (i.e. comparisons of A < B and B < C then A < C and C > B and B > A, etc.)

1

If the comparator is designed to return a negative, positive or 0 to sort the objects, what does the sort method do with this information? How does the sort method obtain meaningful information from the Comparator definition and perform its operation?

A comparator-based sort method (such as merge sort or bubble sort) needs to repeatedly look at two elements in the collection and decide which one should go "first". That decision is made by the comparator, which returns "smaller", "larger", or "same size".

This information is sufficient, the sort method does not need to understand what kind of objects are being sorted. It can just re-arrange the order of elements in the collection until all elements to the "left" are "smaller" than elements to the "right".

This approach does not work for something like a "bucket sort" which needs to assign am absolute numeric value to each element itself (not just a relative value compared to another element).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.