1

This is a snippet of code in Spark/Scala:

 rdd.sortBy(_._2)

What does _._2 mean here?

philantrovert
  • 9,365
  • 3
  • 32
  • 56
min heo
  • 131
  • 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
  • 20,861
  • 6
  • 47
  • 70