import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
/*
Program goal:
-------------
Sort a list of date strings in the format "YYYY-MM-DD".
Approach:
1. Parse each string into LocalDate using a strict formatter.
2. Sort using Scala’s built-in sortBy (O(n log n)).
3. Convert back to strings and print results.
Architecture notes:
-------------------
- Parsing is isolated in a dedicated function for clarity and testability.
- Sorting uses sortBy, which is expressive and efficient.
- Error handling: invalid formats are detected and reported.
- Edge cases: malformed strings, invalid ranges, leap years.
- Performance: LocalDate parsing is fast; sorting is O(n log n).
- Memory: minimal; only a list of small objects.
- Security: avoids unsafe operations; uses immutable LocalDate.
*/
object SortDates {
// Strict ISO 8601 formatter
private val IsoFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE
/*
Parse a date string into LocalDate.
Returns Either[String, LocalDate] for safe error handling.
*/
def parseDate(s: String): Either[String, LocalDate] = {
try {
Right(LocalDate.parse(s, IsoFormatter))
} catch {
case _: DateTimeParseException =>
Left(s"Invalid date: $s")
}
}
/*
Sort a list of date strings.
Returns Either[String, List[String]] to propagate errors.
*/
def sortDates(dates: List[String]): Either[String, List[String]] = {
// Parse all dates; fail fast on invalid input.
val parsed: List[Either[String, LocalDate]] = dates.map(parseDate)
// Check for errors
parsed.collectFirst { case Left(err) => err } match {
case Some(error) => Left(error)
case None =>
val validDates: List[LocalDate] = parsed.collect { case Right(d) => d }
val sorted: List[LocalDate] = validDates.sortBy(identity)
Right(sorted.map(_.toString))
}
}
/*
Helper to print a list of strings.
*/
def printList(list: List[String]): Unit = {
list.foreach(s => println(s" $s"))
}
/*
Helper to run a named test case.
*/
def runTest(name: String, dates: List[String]): Unit = {
println(s"\n=== $name ===")
sortDates(dates) match {
case Right(sorted) => printList(sorted)
case Left(error) => println(s"Error: $error")
}
}
/*
Main program with multiple test cases.
*/
def main(args: Array[String]): Unit = {
println("Scala Date Sorting Demo")
// Basic chronological order
runTest(
"Basic chronological order",
List("2024-01-05", "2023-12-31", "2024-01-01", "2022-07-15")
)
// Already sorted input
runTest(
"Already sorted input",
List("2020-01-01", "2020-01-02", "2020-01-03")
)
// Leap year edge cases
runTest(
"Leap year edge cases",
List("2020-02-29", "2019-02-28", "2021-03-01")
)
// Invalid date detection
runTest(
"Invalid date detection",
List("2024-13-01", "2024-02-30", "not-a-date")
)
}
}
/*
run:
Scala Date Sorting Demo
=== Basic chronological order ===
2022-07-15
2023-12-31
2024-01-01
2024-01-05
=== Already sorted input ===
2020-01-01
2020-01-02
2020-01-03
=== Leap year edge cases ===
2019-02-28
2020-02-29
2021-03-01
=== Invalid date detection ===
ERROR!
Error: Invalid date: 2024-13-01
*/