Scala

Scala

Made by DeepSource

Consider filtering first and then sorting SC-P1003

Performance
Major

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.

Bad practice

val subset = elements.sortWith(_ < _).filter(x => shouldSelect(x))

Recommended

val subset = elements.filter(x => shouldSelect(x)).sortWith(_ < _)