v Sorting Sequences - Scala

Sorting Sequences

If you want to learn more, check out Scala Cookbook and Programming in Scala.

Create Two Vectors

// Create two vectors
val ages = Vector(23,42,12,34)
val lastName = Vector("Jackson", "Dillan", "Bower", "Stein")

Sort Alphabetically

// View the sequence alphabetically
lastName.sorted
Vector(Bower, Dillan, Jackson, Stein)

Sort Ascending

// View the sequence in ascending order
ages.sorted
Vector(12, 23, 34, 42)

Sort Descending

// View the sequence sorted using i > j
ages.sortWith(_ > _)
Vector(42, 34, 23, 12)

Sort By Length

// Voew the sequence sorted by descending length
lastName.sortWith(_.length > _.length)
Vector(Jackson, Dillan, Bower, Stein)