Skip to Content
Scala Functional Programming Patterns
book

Scala Functional Programming Patterns

by Atul S. Khot
December 2015
Intermediate to advanced content levelIntermediate to advanced
298 pages
5h 30m
English
Packt Publishing
Content preview from Scala Functional Programming Patterns

Sorting it out!

Scala provides sorted, sortBy and sortWith methods. We will take a quick look at them and then tackle an interesting memoization problem.

Sorted

The sorted method uses natural ordering amongst the elements of the collection. Natural ordering well, seems natural.

For example, given the following numbers:

1, 11, 22, 2

The natural ordering would be:

1, 2, 11, 22

And the alphanumeric ordering would be:

1, 11, 2, 22

Alternatively, please see http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ for more on natural ordering.

Here is sorted in action:

scala> import scala.util.Random
import scala.util.Random
scala> val list = List.fill(10)(Random.nextInt(100))
list: List[Int] = List(5, 49, 37, 56, 54, 64, 9, 85, 76, 28)
scala> list.sorted ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in Scala

Functional Programming in Scala

Runar Bjarnason, Paul Chiusano
Scala Programming Projects

Scala Programming Projects

Mikael Valot, Nicolas Jorand

Publisher Resources

ISBN: 9781783985845Supplemental Content