forked from divScorp/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparatorMovie.java
More file actions
64 lines (53 loc) · 1.51 KB
/
ComparatorMovie.java
File metadata and controls
64 lines (53 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ObjectOrdering;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class MovieCom {
private double rating;
private String name;
private int year;
public MovieCom(String n, double rt, int y) {
this.name = n;
this.rating = rt;
this.year = y;
}
public double getRating() {
return rating;
}
public String getName() {
return name;
}
public int getYear() {
return year;
}
public String toString() {
return "Name: "+name+"\tRating: "+rating+"\tYear: "+year;
}
}
class RatingCompare implements Comparator<MovieCom> {
public int compare(MovieCom m1, MovieCom m2) {
return (m1.getRating() < m2.getRating()) ? -1 : ((m1.getRating() > m2.getRating()) ? 1 : 0);
}
}
class NameCompare implements Comparator<MovieCom> {
public int compare(MovieCom m1, MovieCom m2) {
return m1.getName().compareTo(m2.getName());
}
}
public class ComparatorMovie {
public static void main(String[] args) {
ArrayList<MovieCom> mov = new ArrayList<>();
mov.add(new MovieCom("Inception ", 8.9, 2010));
mov.add(new MovieCom("The Dark Knight", 9.0, 2008));
mov.add(new MovieCom("Intersteller", 8.7, 2014));
mov.add(new MovieCom("The Prestige", 8.2, 2005));
System.out.println("Sorted Movie list on the basis of Name.");
Collections.sort(mov,new NameCompare());
for(MovieCom m:mov)
System.out.println(m);
System.out.println("\nSorted Movie list on the basis of Rating.");
Collections.sort(mov,new RatingCompare());
for(MovieCom m:mov)
System.out.println(m);
}
}