I'm confused why a type that implements comparable
isn't "implicitly comparable", and also why certain syntaxes of sortWith
won't compile at all:
// The iter is a collection of records, each event_time is a java.sql.Timestamp
// A. Works but won't sort eq millis
val records = iter.toArray.sortWith(_.event_time.getTime < _.event_time.getTime)
// B. Doesn't compile, implements comparable yet not implicitly comparable?
val records = iter.toArray.sortBy(r => (r.event_time))
// C. Doesn't compile, complains about the 2nd _ though it's like the first line
val records = iter.toArray.sortWith(_.event_time.before(_.event_time))
// D. Doesn't compile, also complains though it's more like the first line
val records = iter.toArray.sortWith(_.event_time.compareTo(_.event_time) < 0)
// E. Works, sorts by epoch millis, then nanos after the milli.
val records = iter.toArray.sortBy(r =>
(r.event_time.getTime, r.event_time.getNanos))
// But this leaves questions about the above uses of sortWith which don't work
Given that the first line works, using two underscores, I don't understand why the compiler complains about the other uses of sortsWith. Is there some syntax which could use the before
method happily? And using timestamp's comparable status somehow with line B?
Maybe I should note I'm using Scala 2.11.12 with JDK 8, because it's for a specific version of Spark on a deployment of AWS EMR.
I can see that with Scala 2.13.6 line B compiles and works, however with 2.11.12 I'm getting:
No implicit Ordering defined for java.sql.Timestamp.
not enough arguments for method sortBy: (implicit ord: scala.math.Ordering[java.sql.Timestamp])List[Playground.Foo].
Unspecified value parameter ord.
I'm unclear how to satisfy what it's requesting.
Then the error line C gives me is:
missing parameter type for expanded function ((x$5) => x$5.event_time.before(((x$6) => x$6.event_time)))
While line D is similar with:
missing parameter type for expanded function ((x$7) => x$7.event_time.compareTo(((x$8) => x$8.event_time)).$less(0))
As noted by an answer below, C and D can be accomplished with another syntax:
val records = iter.toArray.sortWith{case(a,b)=>
a.event_time.before(b.event_time)} // C
val records = iter.toArray.sortWith{case(a,b)=>
a.event_time.compareTo(b.event_time) < 0} // D
Which has gotten me to fixing all the ways shown except line B in 2.11.12 (see https://scastie.scala-lang.org/xAwV3FheQWu8n7OoaZaQGA)