Created
January 7, 2016 10:44
-
-
Save hugolu/5cc98e4a634c679db3c4 to your computer and use it in GitHub Desktop.
[Spark] implement sortByValue() with sortBy()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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