Home > Software engineering >  sorting of ZonedDateTime in Scala
sorting of ZonedDateTime in Scala

Time:09-21

I have the following List of ZonedDateTime which is basically read from xml fields in Scala:

var timestamps =  List[ZonedDateTime]()
timestampNodes.foreach(node => timestamps = timestamps :  ZonedDateTime.parse(node.text, DateTimeFormatter.ISO_OFFSET_DATE_TIME))

What is the best and fastest way to sort the timestamps List so that entries are sorted from oldest to newest?

CodePudding user response:

.sortWith() should work.

import java.time.{ZonedDateTime => ZDT}

val sortedtimestamps: List[ZDT] =
  timestampNodes.map(node => ZDT.parse(node.text))
                .sortWith(_.isBefore(_))

Scala 2.11.12 tested via Scastie.

  • Related