1

This is a snippet of code in Spark/Scala:

 rdd.sortBy(_._2)

What does _._2 mean here?

philantrovert
  • 9,904
  • 3
  • 37
  • 61
min heo
  • 151
  • 10

1 Answers1

5

In Scala _2 is shorthand for accessing second tuple element.

val myTuple = ("first", "second")
myTuple._1 // "first"
myTuple._2 // "second"

In your case all tuples in rdd will be sorted by second element. For example:

val tuples = Vector(("first", "b"),("second", "c"),("third", "a"))
tuples.sortBy(_._2) //Vector((third,a), (first,b), (second,c))
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76