1
$\begingroup$

I have a list of the form

{
{1, {3., ∞, ∞, ∞}},
{2, {1.,1., ∞, ∞}}, 
{3, {0.333333, 0.666667, ∞, ∞}}
}

and I would like to sort it by the minimal of the nested list inside each element, namely, Min[{1.,1., ∞, ∞}] etc. The result should be

{
{3, {0.333333, 0.666667, ∞, ∞}}
{2, {1.,1., ∞, ∞}}, 
{1, {3., ∞, ∞, ∞}},
}

I tried SortBy[x,Min[Last]] but the result is unexpected. Any help?

$\endgroup$
2
  • 2
    $\begingroup$ SortBy[lis, Min[Last]] works for me on V 12. Why did it not work for you? $\endgroup$
    – Nasser
    Commented Feb 15, 2020 at 21:18
  • $\begingroup$ @Nasser Not sure. It works in some cases but not in all cases. $\endgroup$
    – wooohooo
    Commented Feb 15, 2020 at 23:59

1 Answer 1

4
$\begingroup$
lst = {{1, {3., ∞, ∞, ∞}}, {2, {1., 1., ∞, ∞}}, {3, {0.333333, 0.666667, ∞, ∞}}};

You can use Min@*Last or Min[Last@#] &

SortBy[Min@*Last]@lst

{{3,{0.333333,0.666667,∞,∞}},{2,{1.,1.,∞,∞}},{1,{3.,∞,∞,∞}}}

or

SortBy[Min[Last@#] &]@lst

{{3,{0.333333,0.666667,∞,∞}},{2,{1.,1.,∞,∞}},{1,{3.,∞,∞,∞}}}

$\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.