Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order a list by the `snd` of a tuple within that list:

Tags:

list

haskell

I have the following list of tuples:

[("a",6),("b",1),("c",2),("d",4),("e",1),("f",1),("g",5),("h",3),("i",1),("j",2)]

but would like to order the tuples in the list by the snd element of the tuple. That way, I get an answer similar to:

[("b",1),("e",1),("f",1),("i",1),("c",2),("j",2),("h",3),("d",4),("g",5),("a",6)]

(i.e. the list is ordered by the second (snd) of each tuple.

like image 330
maclunian Avatar asked Dec 05 '22 21:12

maclunian


1 Answers

sortBy (comparing snd)

where sortBy is in List and comparing is in Data.Ord.

like image 138
Josh Lee Avatar answered Feb 05 '23 21:02

Josh Lee