我的职能是:
def getTime() : ArrayBuffer[Timestamp] = {
val offset = Timestamp.valueOf("2015-01-01 00:00:00").getTime()
val end = Timestamp.valueOf("2015-01-02 00:00:00").getTime()
val diff = end - offset + 1
val mList = ArrayBuffer[Timestamp]()
val numRecords = 3
var i = 0
while (i < numRecords) {
val rand = new Timestamp(offset + (Math.random() * diff).toLong)
mList += rand
i += 1
}
// mList.toList.sortWith(_ < _);
// scala.util.Sorting.quickSort(mList.toArray);
}我试图对数组进行排序,但没有。我知道这个错误:
No implicit Ordering defined for java.sql.Timestamp.我知道我需要确定下订单的方式。是否有一种方法可以像在Java中那样轻松地对其排序:Collections.sort(列表);或者有一种更好的使用Scala的方法?
发布于 2015-05-01 11:48:39
或者,在班上的某个地方定义它,这样就可以了:
implicit def ordered: Ordering[Timestamp] = new Ordering[Timestamp] {
def compare(x: Timestamp, y: Timestamp): Int = x compareTo y
}
getTime().sorted // now this will work just fine发布于 2015-05-01 11:38:28
mList.sortWith(_.compareTo(_) < 1)注意,对于匿名函数,可以传递一个显式函数,如下所示:
def comparator(first: Timestamp, second: Timestamp) = first.compareTo(second) < 1
mList.sortWith(comparator)时间戳本身没有隐含的排序,这里我们只是使用compareTo方法进行排序。
感谢@Nick指出getTime()上的排序在所有情况下都是不够的。我还查看了您希望使用的before方法,但这也只是比较了使用划时代值的情况。
https://stackoverflow.com/questions/29985911
复制相似问题