Let us begin. Here we invoke the sorted function. With no arguments, this returns a List containing the same elements, but sorted in ascending order (from low to high).
object Program {
def main(args: Array[String]): Unit = {
// An example list.
val positions = List(300, 20, 10, 300, 30)
// Sort Ints from low to high (ascending).
val result = positions.sorted
println(result)
// Sort Ints in reverse order (from high to low, descending).
val result2 = positions.sorted(Ordering.Int.reverse)
println(result2)
// The original list is unchanged.
println(positions)
}
}
List(10, 20, 30, 300, 300)
List(300, 300, 30, 20, 10)
List(300, 20, 10, 300, 30)