Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Scala program to sort a List of items. The “sorted()” function will sort the list of items based on their natural ordering.
Let’s look at the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.clearurdoubt object ListSortDemo { def main(args: Array[String]) = { val numbers = List(12, 10, 9, 1, 15, 11, 8, 4) val strings = List("Gowtham", "Avinash", "Prajith", "Karthik") println("Numbers: \n" + numbers) println("Sorted Numbers: \n" + numbers.sorted) println("Strings: \n" + strings) println("Sorted Strings: \n" + strings.sorted) } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.