Searching and Sorting code in Scala