Reputation: 111
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
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);
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
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
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