/
SortByTest.java
75 lines (69 loc) · 2.45 KB
/
SortByTest.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
65
66
67
68
69
70
71
72
73
74
75
package com.annimon.stream.streamtests;
import com.annimon.stream.Stream;
import com.annimon.stream.Student;
import com.annimon.stream.Students;
import com.annimon.stream.function.Function;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements;
import static org.hamcrest.Matchers.contains;
public final class SortByTest {
@Test
public void testSortByStringLength() {
Stream.of("This", "is", "a", "test")
.sortBy(new Function<String, Integer>() {
@Override
public Integer apply(String value) {
return value.length();
}
})
.custom(assertElements(contains(
"a", "is", "This", "test"
)));
}
@Test
public void testSortByStudentName() {
final List<Student> students = Arrays.asList(
Students.STEVE_CS_4,
Students.MARIA_ECONOMICS_1,
Students.VICTORIA_CS_3,
Students.JOHN_CS_2
);
Stream.of(students)
.sortBy(new Function<Student, String>() {
@Override
public String apply(Student student) {
return student.getName();
}
})
.custom(assertElements(contains(
Students.JOHN_CS_2,
Students.MARIA_ECONOMICS_1,
Students.STEVE_CS_4,
Students.VICTORIA_CS_3
)));
}
@Test
public void testSortByStudentCourseDescending() {
final List<Student> students = Arrays.asList(
Students.STEVE_CS_4,
Students.MARIA_ECONOMICS_1,
Students.VICTORIA_CS_3,
Students.JOHN_CS_2
);
Stream.of(students)
.sortBy(new Function<Student, Integer>() {
@Override
public Integer apply(Student student) {
return -student.getCourse();
}
})
.custom(assertElements(contains(
Students.STEVE_CS_4,
Students.VICTORIA_CS_3,
Students.JOHN_CS_2,
Students.MARIA_ECONOMICS_1
)));
}
}