0

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?

John
  • 833
  • 6
  • 23
  • 2
    https://stackoverflow.com/questions/38059191/how-make-implicit-ordered-on-java-time-localdate – Thilo Jun 13 '19 at 09:07
  • 1
    well, the simples way I think will be to convert date to epoch time, so its will be as long type, this will solve issue with missing implicit – Pavel Jun 13 '19 at 09:10

2 Answers2

2

It looks like you need an Ordering for your java.time.LocalDate - see this answer

implicit val localDateOrdering: Ordering[LocalDate] = Ordering.by(_.toEpochDay)
Valy Dia
  • 2,568
  • 2
  • 10
  • 29
1

This should do it.

.sortWith(getFileDate(_._1) isBefore getFileDate(_._1))
jwvh
  • 46,953
  • 7
  • 29
  • 59