按日期降序排序,如果相同,则按姓名排序,如果相同,则按英语、数学和科学的最高分排序。
object OptionAnswer {
case class Score(
name: String,
english: Int,
math: Int,
science: Int,
date: LocalDate
)
def main(args: Array[String]): Unit = {
val scoreSeq: Seq[Score] = List(scoreOfAlice,scoreOfBob,scoreOfCharlie,scoreOfDave)
val keys = Seq(-5, 1, 2, 3, 4)
println(sortScore(scoreSeq, keys))
}
def sortScore(scoreSeq: Seq[Score], keys: Seq[Int]): Seq[Score] = {
val keys_set = keys.toSet
keys_set match{
case 1 => scoreSeq.sortBy(score => score.name)
case 2 => scores.sortBy(score => -score.english)
case 3 => scores.sortBy(score => -score.math)
case 4 => scores.sortBy(score => -score.science)
case -1 => scoreSeq.sortBy(score => -score.name)
case -2 => scores.sortBy(score => score.english)
case -3 => scores.sortBy(score => score.math)
case -4 => scores.sortBy(score => score.science)
case _ => score
}
1条答案
按热度按时间h79rfbju1#
首先,需要编写一个函数来按单个键进行排序:
然后,您可以使用
foldLeft
将排序应用于键列表:关键字为
distinct
,用于删除重复项;关键字为reverse
,因为最重要的关键字是最后应用的关键字。另一种方法是使用键列表创建一个复杂的排序函数,但我会让其他人来编写解决方案。