Sort a list of pairs using sort Haskell
haskell
Solution
import Data.Function (on)
import Data.List (sortBy)
results xs = sortBy (compare `on` fst) (frequency xs)
-- or, if you prefer
results xs = sort (frequency xs)
Links to documentation for `on`, `sortBy`, `compare`, `fst`.
The difference is that `sort` sorts in ascending order of the first element of each pair, breaking tie-breaks with the second elements of the pairs, while `sortBy (compare `on` fst)` explicitly only looks at the first element of each pair.
Problem
I have a function (frequency) which that counts how many times each distinct value in a list occurs in that list. For example, ``` frequency "ababca" ``` should return: ``` [(3, 'a'), (2, 'b'), (1, 'c')]. ``` This works fine but now I need to sort the list using the first element within the list of the list using this function. ``` results :: [Party ] -> [(Int, Party)] results xs = ??? frequency (sort xs) ??? ``` example of desired output: ``` [(1, "Green"), (2, "Red"), (3, "Blue")] ``` The above does not work, I have no idea what I can do. using regular 'sort' Thank you in Advance.