sortOn package:containers

sortOn sorts the specified Seq by comparing the results of a key function applied to each element. The sort is stable, meaning the order of equal elements is preserved. sortOn f is equivalent to sortBy (compare `on` f), but has the performance advantage of only evaluating f once for each element in the input Seq. An example of using sortOn might be to sort a Seq of strings according to their length:
sortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
If, instead, sortBy had been used, length would be evaluated on every comparison, giving <math> evaluations, rather than <math>. If f is very cheap (for example a record selector, or fst), sortBy (compare `on` f) will be faster than sortOn f.
unstableSortOn sorts the specified Seq by comparing the results of a key function applied to each element. unstableSortOn f is equivalent to unstableSortBy (compare `on` f), but has the performance advantage of only evaluating f once for each element in the input Seq. An example of using unstableSortOn might be to sort a Seq of strings according to their length:
unstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
If, instead, unstableSortBy had been used, length would be evaluated on every comparison, giving <math> evaluations, rather than <math>. If f is very cheap (for example a record selector, or fst), unstableSortBy (compare `on` f) will be faster than unstableSortOn f.