-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortStudents.java
64 lines (52 loc) · 1.78 KB
/
SortStudents.java
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
import java.io.IOException;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class SortStudents {
public static void main(String[] args) throws IOException {
List<Student> students = new StudentsFile("students.csv").getStudents();
printStudents(sortByNaturalOrdering(students));
}
public static void printStudents(
List<Student> students) {
for (Student student: students) {
System.out.println(student);
}
}
public static List<Student> sortByNaturalOrdering(
List<Student> students) {
Collections.sort(students);
return students;
}
public static List<Student> sortByGradeComparator(
List<Student> students) {
Collections.sort(students, new GradeComparator());
return students;
}
public static List<Student> sortByGradeAnonymousClass(
List<Student> students) {
Collections.sort(students,
new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int comp = Float.compare(s1.getGrade(), s2.getGrade());
if (0 == comp) {
comp = s1.getGTID() - s2.getGTID();
}
return comp;
}
});
return students;
}
public static List<Student> sortByGradeLambda(
List<Student> students) {
Collections.sort(students,
(Student s1, Student s2) -> {
int comp = Float.compare(s1.getGrade(), s2.getGrade());
if (0 == comp) {
comp = s1.getGTID() - s2.getGTID();
}
return comp;
});
return students;
}
}