2
$\begingroup$

I have a list like this:

L1 = {{500, {x->3, y-> 4, z -> 1}}, {110, {x->11, y->41, z -> 9}}, {750, {x-> 1, y->-1, z->0}}}

Is there a way to pull out the element with the largest element in the first position? So, in this case, we would have returned:

{750, {x-> 1, y->-1, z->0}

Thank you.

$\endgroup$
1
  • 2
    $\begingroup$ MaximalBy[L1, First] or Last@Sort@L1 $\endgroup$ Oct 30, 2019 at 4:29

1 Answer 1

4
$\begingroup$

You can use a combination of Part and Ordering:

L1[[Ordering[L1[[All, 1]], -1]]]

{{750, {x -> 1, y -> -1, z -> 0}}}

or a combination of Part and OrderingBy:

L1[[OrderingBy[L1, First, -1]]]
{{750, {x -> 1, y -> -1, z -> 0}}}

For large lists the first approach is faster.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.