Scala sort list of tuples by attribute
list, scala, tuples
Solution
We could run a performance test?
val items = List("a" -> 0, "b" -> 1, "c" -> 0, "d" -> 0, "e" -> 1)
items.groupBy(_._2).toList
.sortBy(_._2.head._1)(new Ordering[String]() {
override def compare(x: String, y: String) = { -x.compareTo(y) }
})
.map(e => (e._2.head._1 -> e._1))
Result:
List((b,1), (a,0))
Problem
I was wondering what's an easy way to sort a `List[(String, String)]` by the first string in the tuple alphabetically while removing all duplicates for the second string in the tuple. Thanks for the suggestions!