LoginSignup
4
4

More than 5 years have passed since last update.

scalaでソート

Posted at

集計とかするときに使えるかも

タプルだとsortByが使える

scala> val list = List(("aaa",2),("bbb",1),("ccc",4))
list: List[(String, Int)] = List((aaa,2), (bbb,1), (ccc,4))
scala> list.sortBy(_._2)
List[(String, Int)] = List((bbb,1), (aaa,2), (ccc,4))

マップ型だとtoSeqでSeqにしてあげる必要がある

scala> val list = Map("aaa" -> 3, "bbb" -> 1, "ccc" -> 4, "ddd" ->2 )
scala.collection.immutable.Map[String,Int] = Map(aaa -> 3, bbb -> 1, ccc -> 4, ddd -> 2)
scala> list.toSeq
Seq[(String, Int)] = ArrayBuffer((aaa,3), (bbb,1), (ccc,4), (ddd,2))
scala> list.toSeq.sortBy(_._2)
Seq[(String, Int)] = ArrayBuffer((bbb,1), (ddd,2), (aaa,3), (ccc,4))

scala難しい。。。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4