25 def pickFirst2(arr: Array[Int], criteria: Int => Boolean): Option[Int] =
26 arr.filter(criteria).headOption
27 def pickInAnOrder(arr: Array[Int], criteria: Int => Boolean): Array[Int] =
28 arr.sortWith(_ < _).filter(criteria)29
30 def readFile(path: String): String = Source.fromFile(path).mkString
31
Algorithms such as sorting and filtering depend upon size of a structure, i.e. the number of elements in a structure. If you wish to arrange your elements in a specified order and select only a subset of these elements, it is suggested that you first filter the elements according to your criteria and then sort them as this potentially reduces the number of elements to be sorted, thus reducing the overall time spent performing this operation.
val subset = elements.sortWith(_ < _).filter(x => shouldSelect(x))
val subset = elements.filter(x => shouldSelect(x)).sortWith(_ < _)