sortOn haskell

The sortOn function in Haskell is used to sort a list based on a specific key. It takes two arguments: the key function and the list to be sorted. The key function is applied to each element of the list, and the resulting values are used for comparison during the sorting process. The sortOn function is part of the Data.List module.

Here is an example usage of sortOn:

import Data.List (sortOn)

data Person = Person { name :: String, age :: Int }

people :: [Person]
people = [Person "Alice" 25, Person "Bob" 30, Person "Charlie" 20]

sortedPeople = sortOn age people

In this example, the sortOn function is used to sort the people list based on the age field of each Person object. The resulting sortedPeople list will contain the Person objects sorted in ascending order of their ages.

Please let me know if you need further assistance!