Scala:如何按元组的第二个元素对元组数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2627919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: how can I sort an array of tuples by their second element?
提问by pau.estalella
is there a way in Scala to sort an array of tuples using and arbitrary comparison function? In particular I need to sort and array of tuples by their second element, but I wanted to know a general technique to sort arrays of tuples.
在 Scala 中有没有办法使用和任意比较函数对元组数组进行排序?特别是我需要按元组的第二个元素对元组数组进行排序,但我想知道一种对元组数组进行排序的通用技术。
Thanks!
谢谢!
采纳答案by Michel Kr?mer
You can use this code:
您可以使用此代码:
scala> val v = Array(('a', 2), ('b', 1))
v: Array[(Char, Int)] = Array((a,2), (b,1))
scala> scala.util.Sorting.stableSort(v,
| (e1: (Char, Int), e2: (Char, Int)) => e1._2 < e2._2)
scala> v
res11: Array[(Char, Int)] = Array((b,1), (a,2))
Unfortunetly, it seems that Scala cannot infer the type of the array passed to stableSort. I hope that's ok for you.
不幸的是,Scala 似乎无法推断传递给stableSort. 我希望这对你没问题。
回答by Eastsun
In scala 2.8, there is a method sortBy. Here is a simple use case:
在 Scala 2.8 中,有一个 sortBy 方法。这是一个简单的用例:
scala> val arr = Array(("One",1),("Two",2),("Four",4),("Three",3))
arr: Array[(java.lang.String, Int)] = Array((One,1), (Two,2), (Four,4), (Three,3))
scala> arr.sortBy(_._2)
res0: Array[(java.lang.String, Int)] = Array((One,1), (Two,2), (Three,3), (Four,4))
scala>
回答by Erik Kaplun
If it's an Array, it's probably typical to use in-place sorting algorithms. However, in idiomatic Scala code, mutable collections are usually not encouraged/used. If that's the case and you have am immutable collection (or would like to not modify the Arrayin place), use sortWith:
如果是Array,则可能通常使用就地排序算法。然而,在惯用的 Scala 代码中,通常不鼓励/使用可变集合。如果是这种情况并且您有不可变的集合(或不想修改Array就地),请使用sortWith:
scala> val a = Array(1, 3, 2, 5)
a: Array[Int] = Array(1, 3, 2, 5)
scala> a.sortWith(_ > _)
res6: Array[Int] = Array(5, 3, 2, 1)
scala> a
res7: Array[Int] = Array(1, 3, 2, 5)
sorting an Arrayor any other collection of tuples:
对一个Array或任何其他元组集合进行排序:
scala> val a = Array(('a', 1), ('b', 4), ('c', 5), ('d', 2))
a: Array[(Char, Int)] = Array((a,1), (b,4), (c,5), (d,2))
scala> a.sortWith(_._2 > _._2)
res4: Array[(Char, Int)] = Array((c,5), (b,4), (d,2), (a,1))
scala> a
res5: Array[(Char, Int)] = Array((a,1), (b,4), (c,5), (d,2))
回答by Daniel C. Sobral
On Scala 2.8 (yes, again :), you can also do this:
在 Scala 2.8(是的,再次:),你也可以这样做:
val v = Array(('a', 2), ('b', 1))
scala.util.Sorting.stableSort(v)(manifest[(Char, Int)], Ordering.by(_._2))
In the specific case of pairs, this can also work to sort firstby the second element, and then by the first:
在对的特定情况下,这也可以首先按第二个元素排序,然后按第一个元素排序:
scala.util.Sorting.stableSort(v)(manifest[(Char, Int)], Ordering.by(_.swap))
回答by user unknown
2.7 and not in place:
2.7 并没有到位:
(Array((2,3), (4,2), (1,5)).toList.sort (_._2 < _._2)).toArray
回答by tstenner
You probably want def stableSort[K](a : Seq[K], f : (K, K) => Boolean) : Array[K]from scala.util.Sorting.
Your comparison function would be something like _._2 < _._1
您可能希望def stableSort[K](a : Seq[K], f : (K, K) => Boolean) : Array[K]从 scala.util.Sorting 中获取。
您的比较功能将类似于_._2 < _._1
回答by Jawher
val l = List((2, 1), (3, 2), (0, 3))
l sort { case(a, b) => a > b }

