@@ -2,20 +2,20 @@ package com.baeldung.listofdates
2
2
3
3
import org.junit.jupiter.api.Assertions.assertEquals
4
4
import org.junit.jupiter.api.Test
5
+ import java.text.SimpleDateFormat
5
6
import java.time.LocalDate
6
7
import java.time.format.DateTimeFormatter
7
8
import kotlin.test.assertNotNull
8
9
9
10
class SortingListOfStringDatesUnitTest {
10
11
11
12
@Test
12
- fun `given a list of string dates when sorting with sortedDescending then a sorted list is generated` () {
13
+ fun `given a list of string dates when sorting with DateTimeFormatter then a sorted list is generated` () {
13
14
val dates = listOf (" 31-12-2023" , " 01-01-2023" , " 15-06-2023" )
14
15
15
- val formatter = DateTimeFormatter .ofPattern(" dd-MM-yyyy" )
16
- val sortedDates = dates.map { LocalDate .parse(it, formatter) }
17
- .sortedDescending()
18
- .map { it.format(formatter) }
16
+ val dateFormat = SimpleDateFormat (" dd-MM-yyyy" )
17
+
18
+ val sortedDates = dates.sortedByDescending { dateFormat.parse(it) }
19
19
20
20
val expectedSortedDates = listOf (" 31-12-2023" , " 15-06-2023" , " 01-01-2023" )
21
21
@@ -24,14 +24,11 @@ class SortingListOfStringDatesUnitTest {
24
24
}
25
25
26
26
@Test
27
- fun `given a list of string dates when sorting with sortedByDescending then a sorted list is generated` () {
27
+ fun `given a list of string dates when sorting with LocalDate then a sorted list is generated` () {
28
28
val dates = listOf (" 31-12-2023" , " 01-01-2023" , " 15-06-2023" )
29
29
30
- val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter .ofPattern(" dd-MM-yyyy" )
31
-
32
- val sortedDates = dates.sortedByDescending {
33
- LocalDate .parse(it, dateTimeFormatter)
34
- }
30
+ val dateFormatter = DateTimeFormatter .ofPattern(" dd-MM-yyyy" )
31
+ val sortedDates = dates.sortedByDescending { LocalDate .parse(it, dateFormatter) }
35
32
36
33
val expectedSortedDates = listOf (" 31-12-2023" , " 15-06-2023" , " 01-01-2023" )
37
34
0 commit comments