为什么 Scala 列表没有排序? [英] Why don't Scala Lists have an Ordering?
问题描述
Scala 中没有对列表进行隐式排序的原因是什么?
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 implicit value for parameter ord: Ordering[List[Int]]
编辑
是的,我的问题是为什么没有已经隐含在范围内的内置排序.对我来说,很明显第二个列表应该小于"第一个列表,因为 0 处的项目相等,而第二个列表的较低项目为 1.我想知道是否可能是因为当列表有两种不同的大小.
Yes, my question is why there is no built-in Ordering that's already implicitly in scope. To me, it seems obvious that the second list should be "less than" the first list since the items at 0 are equal and the second list has the lower item at 1. I was wondering if maybe it's that there's no good answer when the Lists are two different sizes.
推荐答案
我认为这是一个疏忽.字典顺序在 Seqs 上确实有意义.我们应该将它添加到标准库中.
I think it's an oversight. Lexicographic ordering does make sense on Seqs. We should add it to the standard library.
这篇关于为什么 Scala 列表没有排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!