Haskell - Sort List of Tuples by the Last Tuple Element [duplicate]
This question already has an answer here:
sorting lists using sortBy
2 answers
I have been trying to find a way to sort a list [(a,b,c,d),(e,f,g,h)] by the d element.
So far I have tried: sortBy (compare on
snd) but I cant find a way to get the 4th element and not the 2nd.
My output has to be the same but ordered instead.
Thanks in advance!
haskell
marked as duplicate by jberryman, Community♦ Nov 23 at 10:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
sorting lists using sortBy
2 answers
I have been trying to find a way to sort a list [(a,b,c,d),(e,f,g,h)] by the d element.
So far I have tried: sortBy (compare on
snd) but I cant find a way to get the 4th element and not the 2nd.
My output has to be the same but ordered instead.
Thanks in advance!
haskell
marked as duplicate by jberryman, Community♦ Nov 23 at 10:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
sorting lists using sortBy
2 answers
I have been trying to find a way to sort a list [(a,b,c,d),(e,f,g,h)] by the d element.
So far I have tried: sortBy (compare on
snd) but I cant find a way to get the 4th element and not the 2nd.
My output has to be the same but ordered instead.
Thanks in advance!
haskell
This question already has an answer here:
sorting lists using sortBy
2 answers
I have been trying to find a way to sort a list [(a,b,c,d),(e,f,g,h)] by the d element.
So far I have tried: sortBy (compare on
snd) but I cant find a way to get the 4th element and not the 2nd.
My output has to be the same but ordered instead.
Thanks in advance!
This question already has an answer here:
sorting lists using sortBy
2 answers
haskell
haskell
asked Nov 22 at 18:34
Ceut
64
64
marked as duplicate by jberryman, Community♦ Nov 23 at 10:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jberryman, Community♦ Nov 23 at 10:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
fst
and snd
are provided specifically for 2-tuples as convenience functions. However, you can very easily write a function that works for 4-tuples.
frth :: (a, b, c, d) -> d
frth (_, _, _, d) = d
Then
sortBy (compare `on` frth)
Thank you so much!
– Ceut
Nov 22 at 21:42
Or justsortOn frth
.
– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
=sortBy (comparing f)
=sortOn f
– Jon Purdy
Nov 23 at 7:17
add a comment |
You can define your own projection
get4thOf4 :: (a,b,c,d) -> d
get4thOf4 (_,_,_,d) = d
after which you can use sortBy (comparing get4thof4)
.
Alternatively, use a lambda: sortBy (comparing ((_,_,_,d) -> d))
(comparing ...
means the same thing as compare `on` ...
)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
fst
and snd
are provided specifically for 2-tuples as convenience functions. However, you can very easily write a function that works for 4-tuples.
frth :: (a, b, c, d) -> d
frth (_, _, _, d) = d
Then
sortBy (compare `on` frth)
Thank you so much!
– Ceut
Nov 22 at 21:42
Or justsortOn frth
.
– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
=sortBy (comparing f)
=sortOn f
– Jon Purdy
Nov 23 at 7:17
add a comment |
fst
and snd
are provided specifically for 2-tuples as convenience functions. However, you can very easily write a function that works for 4-tuples.
frth :: (a, b, c, d) -> d
frth (_, _, _, d) = d
Then
sortBy (compare `on` frth)
Thank you so much!
– Ceut
Nov 22 at 21:42
Or justsortOn frth
.
– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
=sortBy (comparing f)
=sortOn f
– Jon Purdy
Nov 23 at 7:17
add a comment |
fst
and snd
are provided specifically for 2-tuples as convenience functions. However, you can very easily write a function that works for 4-tuples.
frth :: (a, b, c, d) -> d
frth (_, _, _, d) = d
Then
sortBy (compare `on` frth)
fst
and snd
are provided specifically for 2-tuples as convenience functions. However, you can very easily write a function that works for 4-tuples.
frth :: (a, b, c, d) -> d
frth (_, _, _, d) = d
Then
sortBy (compare `on` frth)
answered Nov 22 at 18:44
Silvio Mayolo
13.6k22252
13.6k22252
Thank you so much!
– Ceut
Nov 22 at 21:42
Or justsortOn frth
.
– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
=sortBy (comparing f)
=sortOn f
– Jon Purdy
Nov 23 at 7:17
add a comment |
Thank you so much!
– Ceut
Nov 22 at 21:42
Or justsortOn frth
.
– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
=sortBy (comparing f)
=sortOn f
– Jon Purdy
Nov 23 at 7:17
Thank you so much!
– Ceut
Nov 22 at 21:42
Thank you so much!
– Ceut
Nov 22 at 21:42
Or just
sortOn frth
.– Alec
Nov 22 at 23:57
Or just
sortOn frth
.– Alec
Nov 22 at 23:57
sortBy (compare `on` f)
= sortBy (comparing f)
= sortOn f
– Jon Purdy
Nov 23 at 7:17
sortBy (compare `on` f)
= sortBy (comparing f)
= sortOn f
– Jon Purdy
Nov 23 at 7:17
add a comment |
You can define your own projection
get4thOf4 :: (a,b,c,d) -> d
get4thOf4 (_,_,_,d) = d
after which you can use sortBy (comparing get4thof4)
.
Alternatively, use a lambda: sortBy (comparing ((_,_,_,d) -> d))
(comparing ...
means the same thing as compare `on` ...
)
add a comment |
You can define your own projection
get4thOf4 :: (a,b,c,d) -> d
get4thOf4 (_,_,_,d) = d
after which you can use sortBy (comparing get4thof4)
.
Alternatively, use a lambda: sortBy (comparing ((_,_,_,d) -> d))
(comparing ...
means the same thing as compare `on` ...
)
add a comment |
You can define your own projection
get4thOf4 :: (a,b,c,d) -> d
get4thOf4 (_,_,_,d) = d
after which you can use sortBy (comparing get4thof4)
.
Alternatively, use a lambda: sortBy (comparing ((_,_,_,d) -> d))
(comparing ...
means the same thing as compare `on` ...
)
You can define your own projection
get4thOf4 :: (a,b,c,d) -> d
get4thOf4 (_,_,_,d) = d
after which you can use sortBy (comparing get4thof4)
.
Alternatively, use a lambda: sortBy (comparing ((_,_,_,d) -> d))
(comparing ...
means the same thing as compare `on` ...
)
answered Nov 22 at 18:44
chi
72.9k280135
72.9k280135
add a comment |
add a comment |