Skip to content

Commit bb530e3

Browse files
committed
Sort ArrayList
1 parent 30aab97 commit bb530e3

2 files changed

Lines changed: 87 additions & 42 deletions

File tree

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,103 @@
11
package com.hellokoding.java.collections;
22

3+
import org.assertj.core.api.ThrowableAssert;
34
import org.junit.Test;
45

5-
import java.util.*;
6-
import java.util.stream.Collectors;
6+
import java.time.LocalDate;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Comparator;
10+
import java.util.List;
711

812
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
914

1015
public class ArrayListSortingTest {
1116
@Test
12-
public void givenNonComparableAndComparator_whenSortAsc_thenSuccess(){
13-
// given
14-
List<NonComparableBook> bookList = new ArrayList<>();
15-
bookList.add(new NonComparableBook("C"));
16-
bookList.add(new NonComparableBook("A"));
17-
bookList.add(new NonComparableBook("D"));
18-
19-
// when sorting in natural / ascending order
20-
bookList.sort(Comparator.comparing(e -> e.title));
21-
22-
// then
23-
String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining());
24-
assertThat(actual).isEqualTo("ACD");
17+
public void sortStrings(){
18+
List<String> strings = new ArrayList<>(List.of("c", "a", "b"));
19+
20+
strings.sort(Comparator.naturalOrder());
21+
assertThat(strings).containsExactly("a", "b", "c");
22+
23+
strings.sort(Comparator.reverseOrder());
24+
assertThat(strings).containsExactly("c", "b", "a");
25+
}
26+
27+
@Test
28+
public void sortNumbers() {
29+
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
30+
31+
numbers.sort(Comparator.naturalOrder());
32+
assertThat(numbers).containsExactly(1, 2, 3);
33+
34+
numbers.sort(Comparator.reverseOrder());
35+
assertThat(numbers).containsExactly(3, 2, 1);
2536
}
2637

2738
@Test
28-
public void givenComparable_whenSortAsc_thenSuccess(){
29-
// given
30-
List<ComparableBook> bookList = new ArrayList<>();
31-
bookList.add(new ComparableBook("C"));
32-
bookList.add(new ComparableBook("A"));
33-
bookList.add(new ComparableBook("D"));
34-
35-
// when sorting in natural / ascending order
36-
bookList.sort(Comparator.naturalOrder());
37-
38-
// then
39-
String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining());
40-
assertThat(actual).isEqualTo("ACD");
39+
public void sortDates() {
40+
LocalDate ld1 = LocalDate.of(2019, 3, 1);
41+
LocalDate ld2 = LocalDate.of(2019, 1, 1);
42+
LocalDate ld3 = LocalDate.of(2019, 2, 1);
43+
List<LocalDate> dates = new ArrayList<>(List.of(ld1, ld2, ld3));
44+
45+
dates.sort(Comparator.naturalOrder());
46+
assertThat(dates).containsExactly(ld2, ld3, ld1);
47+
48+
dates.sort(Comparator.reverseOrder());
49+
assertThat(dates).containsExactly(ld1, ld3, ld2);
50+
}
51+
52+
@Test
53+
public void sortNullObject() {
54+
List<Integer> lst = new ArrayList<>(Arrays.asList(3, 1, null));
55+
56+
ThrowableAssert.ThrowingCallable c = () -> lst.sort(Comparator.naturalOrder());
57+
assertThatThrownBy(c).isInstanceOf(NullPointerException.class);
4158
}
4259

4360
@Test
44-
public void givenComparable_whenSortDesc_thenSuccess(){
45-
// given
46-
List<ComparableBook> bookList = new ArrayList<>();
47-
bookList.add(new ComparableBook("C"));
48-
bookList.add(new ComparableBook("A"));
49-
bookList.add(new ComparableBook("D"));
50-
51-
// when sorting in reverse / descending order
52-
bookList.sort(Comparator.reverseOrder());
53-
54-
// then
55-
String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining());
56-
assertThat(actual).isEqualTo("DCA");
61+
public void sortOneField() {
62+
Book book1 = new Book(1, "b");
63+
Book book2 = new Book(2, "c");
64+
Book book3 = new Book(3, "a");
65+
66+
List<Book> list = Arrays.asList(book1, book2, book3);
67+
list.sort(Comparator.comparing(Book::getTitle));
68+
69+
assertThat(list).containsExactly(book3, book1, book2);
70+
}
71+
72+
@Test
73+
public void sortMultipleFields() {
74+
Book book1 = new Book(1, "b");
75+
Book book2 = new Book(2, "c");
76+
Book book3 = new Book(3, "c");
77+
78+
List<Book> list = Arrays.asList(book1, book2, book3);
79+
list.sort(Comparator
80+
.comparing(Book::getTitle, Comparator.reverseOrder())
81+
.thenComparing(Book::getId, Comparator.reverseOrder()));
82+
83+
assertThat(list).containsExactly(book3, book2, book1);
84+
}
85+
86+
static class Book {
87+
int id;
88+
String title;
89+
90+
Book(int id, String title) {
91+
this.id = id;
92+
this.title = title;
93+
}
94+
95+
int getId() {
96+
return id;
97+
}
98+
99+
String getTitle() {
100+
return title;
101+
}
57102
}
58103
}

java-examples/java-core/src/test/java/com/hellokoding/java/collections/HashSetSortingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void sortDescWithTreeSet() {
2626
}
2727

2828
@Test
29-
public void sortWithTreeSetAndComparator() {
29+
public void sortDescWithTreeSet2() {
3030
Set<Integer> set = new HashSet<>(Set.of(3, 1, 2));
3131
NavigableSet<Integer> sortedSet = new TreeSet<>(Comparator.reverseOrder());
3232
sortedSet.addAll(set);

0 commit comments

Comments
 (0)