Skip to content

Instantly share code, notes, and snippets.

@hugolu
Created January 7, 2016 10:44
Show Gist options
  • Save hugolu/5cc98e4a634c679db3c4 to your computer and use it in GitHub Desktop.
Save hugolu/5cc98e4a634c679db3c4 to your computer and use it in GitHub Desktop.
[Spark] implement sortByValue() with sortBy()
val fruits = List("Apple", "Banana", "Fig", "Wiki", "Pineapple", "Strawberry", "Watermelon")
val keyValues = sc.parallelize(fruits.map(fruit => (fruit, fruit.length)))
keyValues.sortByKey().foreach(print)
//output> (Apple,5)(Banana,6)(Fig,3)(Pineapple,9)(Strawberry,10)(Watermelon,10)(Wiki,4)
keyValues.sortBy(_._1).foreach(print) //as sortByKey()
//output> (Apple,5)(Banana,6)(Fig,3)(Pineapple,9)(Strawberry,10)(Watermelon,10)(Wiki,4)
keyValues.sortBy(_._2).foreach(print) //as sortByValue()
//output> (Fig,3)(Wiki,4)(Apple,5)(Banana,6)(Pineapple,9)(Strawberry,10)(Watermelon,10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment