-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathComparatorExample.java
More file actions
70 lines (60 loc) · 1.87 KB
/
ComparatorExample.java
File metadata and controls
70 lines (60 loc) · 1.87 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
65
66
67
68
69
70
import java.util.*;
/**
* <h1>JavaEE Online Course</h1>
* <h2>Collections Framework</h2>
* <p>
* These classes represents the usage of Java
* Collection Framework as demonstrated on
* the Online Course conducted by Dimik Computing.
* Feel free to fork and try it on your own.
* </p>
*
* @author Sharif Ahmed (https://github.com/sharifahmed)
* @version 1.0
* @since 2016-07-01
*/
public class ComparatorExample {
public static void main(String args[]) {
// create an array list
List<Person> list = new ArrayList<>();
// adding persons to the list
list.add(new Person("James", 20));
list.add(new Person("John", 15));
list.add(new Person("Harry", 18));
list.add(new Person("Mathews", 45));
list.add(new Person("Andrew", 21));
// displaying the persons list
System.out.println("Person list before sorting: ");
for (Person p : list) {
System.out.println(p.getName());
}
// sorting according to name
Collections.sort(list, new Comparator<Person>() {
public int compare(Person person1, Person person2) {
String name1 = person1.getName().toUpperCase();
String name2 = person2.getName().toUpperCase();
//ascending order
return name1.compareTo(name2);
}
});
// displaying the persons list after sorting by name
System.out.println("Person list after sorting by name: ");
for (Person p : list) {
System.out.println(p.getName() + " " + p.getAge());
}
// sorting according to age
Collections.sort(list, new Comparator<Person>() {
public int compare(Person person1, Person person2) {
int age1 = person1.getAge();
int age2 = person2.getAge();
//ascending order
return age1 - age2;
}
});
// displaying the persons list after sorting by age
System.out.println("Person list after sorting by age: ");
for (Person p : list) {
System.out.println(p.getName() + " " + p.getAge());
}
}
}