[erlang-questions] Sorting of lists

Anders Nygren <>
Thu Apr 19 20:39:09 CEST 2007


On 4/19/07, Jordan Wilberding <> wrote:
> MyList = [ {3, ceasar}, {1, adam}, {4, david}, {2, billy}],
> lists:sort(fun({_,L}, {_,R}) -> L =< R end, MyList).
>

Or simpler
33> MyList = [ {3, ceasar}, {1, adam}, {4, david}, {2, billy}].
[{3,ceasar},{1,adam},{4,david},{2,billy}]
34> lists:keysort(2,MyList).
[{1,adam},{2,billy},{3,ceasar},{4,david}]

The libraries are Your best friends, spend some (actually ALOT) of
time reading the
library documentation, there are lots of useful stuff there.

/Anders

> Thanks!
> Jordan Wilberding
>
> Fredrik Hoback wrote:
> > Hi, I've this problem:
> >
> > If you have a list containing elements like this:
> > Names = [adam, billy, ceasar, david]
> >
> > and then I have another list:
> >
> > MyList = [ {3, ceasar}, {1, adam}, {4, david}, {2, billy}]
> >
> > then I would like to order MyList according to Names, so MyList would
> > look like this:
> >
> > [{1, adam}, {2, billy}, {3, ceasar},  {4, david}].
> >
> > How would a function look like to get MyList sorted according to Names ?
> > ---------------------------------
> > _______________________________________________



More information about the erlang-questions mailing list