gpt4 book ai didi

scala - 排序时间戳的scala arrayBuffer

转载 作者:行者123 更新时间:2023-12-03 18:09:48 26 4
gpt4 key购买 nike

我有这个功能:

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(list);
或者有更好的方法使用 Scala?

最佳答案

或者,在您的类(class)中的某个地方定义它,您就可以开始了:

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

关于scala - 排序时间戳的scala arrayBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com