Why don't Scala Lists have an Ordering?

前端 未结 6 1577
攒了一身酷
攒了一身酷 2020-12-31 00:47

Is there a reason why there is no implicit Ordering for Lists in Scala?

val lists = List(List(2, 3, 1), List(2, 1, 3))
lists.sorted

error: could not find im         


        
6条回答
  •  别那么骄傲
    2020-12-31 01:02

    You can use sortWith. This doesn't take differently sized lists into account because zip will throw out the difference, but I think it does something like what you're after:

    lists.sortWith((a,b) => {
      a.zip(b).filterNot(x => x._1 == x._2) match {
        case Nil => true
        case t => t._1 < t._2
      }
    })
    

提交回复
热议问题