user3189506
user3189506

Reputation: 111

How does compareTo method of Comparable class work for objects

Say I had a Movie class with the following instance variables:

private String myTitle; // title of Bond film
private String myBondActor; // name of actor who portrayed James Bond
private int myYear; // year film was released
private double myFilmRating;// from all-reviews.com
private int myLengthHours; // hours (truncated) portion of film length
private int myLengthMinutes;// minutes beyond truncated hours

and I had an ArrayList of Movie objects, and I used a bubble sort algorithm to sort the ArrayList:

public void bubbleSort(ArrayList<Movie> list) {
for (int outer = 0; outer < list.size() - 1; outer++) {
    for (int inner = 0; inner < list.size() - outer - 1; inner++) {
        if (list.get(inner).compareTo(list.get(inner + 1)) > 0) {
            Movie temp = list.get(inner);
            list.set(inner, list.get(inner + 1));
            list.set(inner + 1, temp);
        }
    }
}

How is the ArrayList being sorted?

Upvotes: 0

Views: 5361

Answers (3)

Oliver
Oliver

Reputation: 6260

See when you do int x= 5,y=10 and we can now compare them like x<y, we can compare two variables, but how do we compare two objects ??

We compare objects by compareTo(), Your Movie class must implement Comparable interface in order to compare its objects with one another.

You have to provide implementation to compareTo() method, this criteria will decide which object is greater than the other.

For Example, Let us say a movie should be sorted(compared) based on int myYear(year of release) of film so implementation will be

public int compareTo(Movie other)
{
   return this.myYear - other.getMyYear();
}

Now we create two objects m1 and m2 of type Movie where m1.myYear=2000 and m2.myYear=2001

when we do m1.compareTo(m2);

  • we get 2000 - 2001 = -1... A Negative number, that means m1 < m2.
  • If we get a positive number then m1 > m2
  • if we get a zero then m1 ==m2

Here I decide which Movie object is greater than the other based on myYear , you may decided on something else maybe more condtions. Whatever the condition may be, remember the number you return +ve , -ve or a 0 (Zero) will decide which object is greater than the other.

Upvotes: 3

Abhhay Naik
Abhhay Naik

Reputation: 79

Implement Comparable interface:

class Movie implements Comparable 
{
    private String myTitle; 
    private String myBondActor; 
    private int myYear; 
    private double myFilmRating;
    private int myLengthHours; 
    private int myLengthMinutes;

    // Override CompareTo Method
    public int compareTo ( Movie m )
    {
         // Here comparison logic
    }
}

Now just call the method like this:

Collections.sort(list);

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272347

You would have to implement the Comparable interface and the compareTo() method yourself, such that you would compare using your object attributes (e.g. title, year etc.)

int compareTo(T o)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Note the various caveats relating to associativity etc.

I presume you're writing the sort yourself, as an exercise. You can use Collections.sort(), which implements QuickSort, and this method allows an optional Comparator object, which allows you to implement different comparisons (e.g. you could sort by name, by actor name, by release date etc. by substituting in different comparators)

Upvotes: 1

Related Questions