John
John

Reputation: 1220

Understanding Comparator call to compare method

I am going through the following code from here:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class HDTV {
    private int size;
    private String brand;

    public HDTV(int size, String brand) {
        this.size = size;
        this.brand = brand;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
}

class SizeComparator implements Comparator<HDTV> {
    @Override
    public int compare(HDTV tv1, HDTV tv2) {
        int tv1Size = tv1.getSize();
        int tv2Size = tv2.getSize();

        if (tv1Size > tv2Size) {
            return 1;
        } else if (tv1Size < tv2Size) {
            return -1;
        } else {
            return 0;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        HDTV tv1 = new HDTV(55, "Samsung");
        HDTV tv2 = new HDTV(60, "Sony");
        HDTV tv3 = new HDTV(42, "Panasonic");

        ArrayList<HDTV> al = new ArrayList<HDTV>();
        al.add(tv1);
        al.add(tv2);
        al.add(tv3);

        Collections.sort(al, new SizeComparator());
        for (HDTV a : al) {
            System.out.println(a.getBrand());
        }
    }
}

When new SizeComparator() method is called inside Collections.sort(), as far as I have understood size of tv1 is first compared to tv2. And here since tv1 size is less than tv2,the compare method will return -1. In the next step, how will the size of tv3 would be compared? Is it like -1 is compared to 42 ?

Upvotes: 0

Views: 1847

Answers (3)

apgp88
apgp88

Reputation: 985

Compare method does not actually sort the list. It is merely used for the comparison of two objects you provide.

Collections.sort method uses standard sorting algorithm and it uses your implemented compare method anytime it needs while sorting. Hence you can not make any assumptions about which objects will be compared.

Upvotes: 0

Ueli Hofstetter
Ueli Hofstetter

Reputation: 2524

Why should -1 be compared to 42? 42 will be compared to 55 or 60 depending on the sorting algorithms used by Collections.sort.

The easiest way to check and see what is happening is to add something add some debug information, for example

@Override
    public int compare(HDTV tv1, HDTV tv2) {

        System.out.println("Comparing " tv1.getSize() + " to " tv2.getSize())

        int tv1Size = tv1.getSize();
        int tv2Size = tv2.getSize();

        if (tv1Size > tv2Size) {
            return 1;
        } else if (tv1Size < tv2Size) {
            return -1;
        } else {
            return 0;
        }
    }

Upvotes: 1

Zielu
Zielu

Reputation: 8552

Compare is done between the objects not the values returned by the other comparison. So then it will be tv2 compare with tv3 (return 1), tv3 with tv1 (return -1).

Upvotes: 1

Related Questions