Reputation: 1611
I have to sort data from the list but I don't know how. I have some code:
mailClient.messages(unreadMessages)
.flatMap(_.files)
.filter(filename => {
filename._1.contains("test") && filename._1.endsWith("csv")
})
.toList
.sortWith((f, f1) => getFileDate(f._1).compareTo(getFileDate(f._1)))
and that is my next function for getDate:
def getFileDate(filename:String):LocalDate = {
val fileParts = filename.split("_")
val day = Integer.parseInt(fileParts(2))
val month = Integer.parseInt(fileParts(3))
val year = Integer.parseInt(fileParts(4))
new LocalDate(year, month, day)
}
I get date from my filename(test_03_05_2019.csv) and trying sort by this dates and got this compile error.
Error:(56, 14) No implicit Ordering defined for java.time.LocalDate.
.sortBy(f => getFileDate(f._1))
Error:(56, 14) not enough arguments for method sortBy: (implicit ord: scala.math.Ordering[java.time.LocalDate])List[(String, String)].
Unspecified value parameter ord.
.sortBy(f => getFileDate(f._1))
Error:(57, 99) type mismatch;
found : Any
required: String
.map { case (filename, csv) => Source.fromString(logger.trace(s"Csv $filename content: {}", csv)) }
How can I do this?
Upvotes: 0
Views: 585
Reputation: 2851
It looks like you need an Ordering
for your java.time.LocalDate
- see this answer
implicit val localDateOrdering: Ordering[LocalDate] = Ordering.by(_.toEpochDay)
Upvotes: 4
Reputation: 51271
This should do it.
.sortWith(getFileDate(_._1) isBefore getFileDate(_._1))
Upvotes: 1