Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
47 lines (41 loc) · 1.72 KB
/
Copy pathMain.java
File metadata and controls
47 lines (41 loc) · 1.72 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
package corejava.collections.ordering;
import java.util.ArrayList;
import java.util.Collections;
// Driver class
class Main
{
public static void main(String[] args)
{
ArrayList<Movie2> list = new ArrayList<Movie2>();
list.add(new Movie2("Force Awakens", 8.3, 2015));
list.add(new Movie2("Star Wars", 8.7, 1977));
list.add(new Movie2("Empire Strikes Back", 8.8, 1980));
list.add(new Movie2("Return of the Jedi", 8.4, 1983));
// Sort by rating : (1) Create an object of ratingCompare
// (2) Call Collections.sort
// (3) Print Sorted list
System.out.println("Sorted by rating");
RatingCompare ratingCompare = new RatingCompare();
Collections.sort(list, ratingCompare);
for (Movie2 movie: list)
System.out.println(movie.getRating() + " " +
movie.getName() + " " +
movie.getYear());
// Call overloaded sort method with RatingCompare
// (Same three steps as above)
System.out.println("\nSorted by name");
NameCompare nameCompare = new NameCompare();
Collections.sort(list, nameCompare);
for (Movie2 movie: list)
System.out.println(movie.getName() + " " +
movie.getRating() + " " +
movie.getYear());
// Uses Comparable to sort by year
System.out.println("\nSorted by year");
Collections.sort(list);
for (Movie2 movie: list)
System.out.println(movie.getYear() + " " +
movie.getRating() + " " +
movie.getName()+" ");
}
}