I am a beginner in Haskell so please bear with me. (Just started learning yesterday!) How can I sort a list of tuples primarily by their first components (highest to smallest) and secondarily by their second components (smallest to highest)? A sample input/output would be:
[(1, "b"), (1, "a"), (2, "b"), (2, "a")] (input)
[(1, "a"), (2, "a"), (1, "b"), (2, "b")] (middle step)
[(2, "a"), (2, "b"), (1, "a"), (1, "b")] (output)
I tried using the following but it gave wrong output:
sortGT a b = GT
sortBy sortGT lst
I am sure that I can do this by using sortBy only, but I can’t figure it out myself. Any help would be highly appreciated!
You need to construct your function
sortGT, so that it compares pairs the way you want it:Using this you get the following results (I used ghci):