Neo Streets
Neo Streets

Reputation: 535

Getting sortBy in haskell to compare the first element

Hi so I have this pretty ugly list which looks like [(Int,(String,Char))...] and I want to use the sortBy function in Haskell to sort by the Int.

So far I have, a is the list

sorted = sortBy(comparing fst) a

but this gives the good old 'comparing' not in scope error message. Is there a way to do what I want?

Upvotes: 1

Views: 1978

Answers (1)

Rein Henrichs
Rein Henrichs

Reputation: 15605

As mentioned in the comments, comparing is exported by Data.Ord, so:

import Data.List (sortBy)
import Data.Ord (comparing)

a = [(2,"foo"),(1,"bar")]

main = print $ sortBy (comparing fst) a

Upvotes: 5

Related Questions