seph
seph

Reputation: 77

Sorting a list of lists by the seconds element

I'm having a hard time when I try to order a list of lists by the second element, something like this

list = [[_,B,_,_,_],[_,A,_,_,_],[_,C,_,_,_]]

into this:

list = [[_,A,_,_,_],[_,B,_,_,_],[_,C,_,_,_]]

I've tried:

sortBy compare $ [([1,2]!!1),([2,3]!!1)]

But it filters the seconds elements and order that into [2,3].

Upvotes: 5

Views: 514

Answers (2)

epsilonhalbe
epsilonhalbe

Reputation: 15967

another idea I came up with would be to simply start sorting at the second element of a list by using tail. I also tried to write it point free - just as an exercise for me.

pfsortBySnd :: (Ord a) => [[a]] -> [[a]]
pfsortBySnd = sortBy second
            where second = comparing tail

sortBySnd :: (Ord a) => [[a]] -> [[a]]
sortBySnd xx = sortBy second xx
             where second x y = compare (tail x) (tail y)

Upvotes: 0

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68152

What you tried to do is sort the list [([1,2]!!1),([2,3]!!1)], which is equivalent to [2, 3], by compare. What you want to do is use sortBy with a function that first gets the second element and then compares:

sortBySecond = sortBy (\ a b -> compare (a !! 1) (b !! 1))

Then take the list you have and apply this function to it:

sortBySecond [[1, 2], [2, 3]]

You can make this function neater by using on from Data.Function:

import Data.Function

sortBySecond = sortBy (compare `on` (!! 1))

You can also use comparing from Data.Ord:

sortBySecond = sortBy $ comparing (!! 1)

Upvotes: 9

Related Questions