Haskell - Sort List of Tuples by the Last Tuple Element [duplicate]












-1















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!










share|improve this 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.




















    -1















    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!










    share|improve this 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.


















      -1












      -1








      -1








      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!










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          2 Answers
          2






          active

          oldest

          votes


















          6














          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)





          share|improve this answer





















          • Thank you so much!
            – Ceut
            Nov 22 at 21:42










          • 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



















          4














          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` ...)






          share|improve this answer




























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            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)





            share|improve this answer





















            • Thank you so much!
              – Ceut
              Nov 22 at 21:42










            • 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
















            6














            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)





            share|improve this answer





















            • Thank you so much!
              – Ceut
              Nov 22 at 21:42










            • 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














            6












            6








            6






            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)





            share|improve this answer












            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)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 18:44









            Silvio Mayolo

            13.6k22252




            13.6k22252












            • Thank you so much!
              – Ceut
              Nov 22 at 21:42










            • 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


















            • Thank you so much!
              – Ceut
              Nov 22 at 21:42










            • 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
















            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













            4














            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` ...)






            share|improve this answer


























              4














              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` ...)






              share|improve this answer
























                4












                4








                4






                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` ...)






                share|improve this answer












                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` ...)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 18:44









                chi

                72.9k280135




                72.9k280135















                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...