Reputation: 21817
I have a list with tuples in Erlang for example:
[{1, "AAA"}, {2, "AAA"}, {3, "AAAAAAAA"}]
How can i get tuple from this list with max first element from this tuples?
Thank you.
Upvotes: 3
Views: 694
Reputation: 21465
In this case lists:max/1 will return what you want. For this to work all tuples must have the same number of elements.
Upvotes: 5
Reputation: 2919
Use lists:keysort/2.
1> lists:keysort(1, [{1, "AAA"}, {2, "AAA"}, {3, "AAAAAAAA"}]).
edit: Seems I read your questions to fast. If you only want one tuple containing a maximum value and your tuples are of same size Arjan should be the accepted answer.
If you only want one term() element containing a maximum value, and if the rule with similar tuple-sizes Arjan stated doesn't apply, I would go with either a lists:foldl/3
or own recursion function.
Sorting the whole list is unnecessary unless you want the whole list sorted. My mistake.
Upvotes: 3