I am trying to sort my list of tuples based on the 4th element in each of the tuples. The fourth element contains a string that is a person\'s name. I want to put tuples that c
While you can figure something like this out for yourself, most of the desired capability is already available via Data.Ord
. If tuples
is your input list, you can just use:
sortBy (comparing name) tuples
where name
is a utility function defines as:
name (_, _, _, _, n, _) = n
This is actually a parametrically polymorphic function, so you could also call it fifth
, or something generic like that.
You can call the above expression and format the output to see that it does approximately what you want:
Prelude Data.Ord Data.List> putStrLn $ unlines $ show <$> sortBy (comparing name) tuples
("B",101,"M",3,"Jon",3.33)
("B",273,"F",1,"Mike",2.66)
("B",203,"R",3,"Rachel",1.66)
("A",200,"P",1,"Rachel",0.0)
("A",999,"N",3,"Rachel",1.33)
("A",100,"Q",3,"Todd",2.0)
("A",549,"D",3,"Todd",2.0)
("B",220,"S",3,"Todd",4.0)
Compared to the OP, this is in the opposite order of what's required, but I'll leave it as an exercise to figure out how to change the sort order. There's a couple of different ways to do that.