Sort A List
Supposing the following list we can sort a variety of ways.
The default behavior of sorted() is to use math.Ordering, which for strings results in a lexographic sort:
sortWith allows you to provide your own ordering utilizing a comparison function:
sortBy allows you to provide a transformation function:
You can always reverse a list, or a sorted list, using `reverse:
Lists can also be sorted using Java method java.util.Arrays.sort and its Scala wrapper scala.util.Sorting.quickSort
Create a List containing n copies of x
To create a collection of n copies of some object x, use the fill method. This example creates a List, but this can work with other collections for which fill makes sense:
List and Vector Cheatsheet
It is now a best-practice to use Vector instead of List because the implementations have better performance Performance characteristics can be found here. Vector can be used wherever List is used.