user11391606
user11391606

Reputation:

Why can i pass a comparator object to the sort method?

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;
        }
    }
}

Upvotes: 1

Views: 308

Answers (2)

Thilo
Thilo

Reputation: 262684

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).

Upvotes: 1

pamcevoy
pamcevoy

Reputation: 1246

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.)

Upvotes: 4

Related Questions