Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a list of based on the 2nd element of a tuple

I have a dictionary and want to convert it to a list. Then I would like to sort the resulting list consisting of {Key, Value} pairs from min to max depending on the 2nd element(Value).

Is there a built in sort method for Lists to handle this or how does one do this?

Thanks

like image 324
some_id Avatar asked Dec 06 '10 19:12

some_id


1 Answers

The function lists:keysort/2 fits like glove for this.

1> lists:keysort(2, [{a,b},{b,a},{b,b}]).

[{b,a},{a,b},{b,b}]

2> lists:keysort(2, [{1,14},{3,10},{2,13}]).

[{3,10},{2,13},{1,14}]

like image 165
D.Nibon Avatar answered Sep 21 '22 05:09

D.Nibon