[Haskell-beginners] sorting by elements in a tuple

Chaddaï Fouché chaddai.fouche at gmail.com
Fri Oct 16 04:42:30 EDT 2009


On Fri, Oct 16, 2009 at 10:37 AM, Chaddaï Fouché
<chaddai.fouche at gmail.com> wrote:
> On Fri, Oct 16, 2009 at 8:07 AM, Daniel Fischer
> <daniel.is.fischer at web.de> wrote:
>> You would achieve that by
>>
>> tuplesort = sortBy (comparing fst)
>>
>> or, eta-expanded,
>>
>> tuplesort xs = sortBy (comparing fst) xs
>>
>> But if you finally want to sort by the second component also (subordinate to sorting by
>> the first component), it's much easier to go the whole way at once and use plain "sort".
>
> tuplesort = sortBy (comparing fst `mplus` comparing snd)
>

Ooops, that's "mappend" rather than "mplus" :

> tuplesort = sortBy (comparing fst `mappend` comparing snd)

(it would be really cool if (++) was a synonym for mappend though as
it used to be)

-- 
Jedaï


More information about the Beginners mailing list