[erlang-questions] Sorting of lists

Bill Allen erlang@REDACTED
Sun Apr 22 14:33:03 CEST 2007


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 ?
>   
I need a little clarification on the specifications:

1. can the size of Names and MyList be arbitrarily large?
2. is there any necessary correlation between the numbers in mylist and 
the resulting sort?
3. can duplicates keys appear as the atom in MyList (i.e. same key but 
different payload)?

The answers might suggest different algorithms.




More information about the erlang-questions mailing list