Created
February 6, 2015 03:58
-
-
Save gkthiruvathukal/32b3189dea5e88c195df to your computer and use it in GitHub Desktop.
Tuple Sort Scala Worksheet
This file contains hidden or 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 wlist = List("alpha", "gamma", "beta", "epsilon") | |
| case class DerivedData(s : String, len : Int) | |
| val tlist = wlist map { n => DerivedData(n, n.length) } | |
| val sortedBy1 = tlist.sortWith( (l, r) => l.s < r.s) | |
| println("sorted by component 1") | |
| sortedBy1 foreach println | |
| val sortedBy2 = tlist.sortWith( (l, r) => l.len < r.len) | |
| println("sorted by component 2") | |
| sortedBy2 foreach println |
This file contains hidden or 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 wlist = List("alpha", "gamma", "beta", "epsilon") | |
| val tlist = wlist map { n => (n, n.length) } | |
| val sortedBy1 = tlist.sortWith( (l, r) => l._1 < r._1) | |
| println("sorted by component 1") | |
| sortedBy1 foreach println | |
| // Trying to sort on field _2 causes Scala compiler to tank. Yikes. | |
| val sortedBy2 = tlist.sortWith( (l, r) => l._2 < r._2) | |
| println("sorted by component 2") | |
| sortedBy2 foreach println |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks like a regression in 2.11.5. Apparently, both 2.10.4 and 2.11.4 can run TupleSort.sc just fine.