2
$\begingroup$

In an ideal world I would have:

list1 = {{2, 0}, {2, -1}, {2, 0}, {2, -1}};
Sort[list1]

{{2, -1}, {2, -1}, {2, 0}, {2, 0}}

while in the real world I have:

list2 = {{1.999999927261, 2.33473182872375 10^-8},
         {2.00000002055154, -0.99999987168595},
         {2.00000013088255, -4.79449275287154 10^-8},
         {2.00000013921051, -1.00000012791526}};
Sort[list2]

{{2., 2.33473 10^-8}, {2., -1.}, {2., -4.79449 10^-8}, {2., -1.}}

So, the question is: how to get the first sort with the second list?

{{2., -1.}, {2., -1.}, {2., -4.79449 10^-8}, {2., 2.33473 10^-8}}

One idea would be to consider the integer part of such numbers, but does that make sense as what? Otherwise, what would you suggest? Thank you!

$\endgroup$
0

2 Answers 2

3
$\begingroup$

one option could be

list2 = {{1.999999927261, 
   2.33473182872375 10^-8}, {2.00000002055154,-0.99999987168595}, 
{2.00000013088255, -4.79449275287154 10^-8}, {2.00000013921051, 
-1.00000012791526}}

Sort[list2, (#1[[1]] <= #2[[1]] && #1[[2]] <= #2[[2]]) &]

Mathematica graphics

$\endgroup$
5
$\begingroup$

Alternatively

SortBy[list2, Last]
(*{{2., -1.}, {2., -1.}, {2., -4.79449*10^-8}, {2.,2.33473*10^-8}}*)
$\endgroup$
1
  • $\begingroup$ +1 If there are multiple clusters, broaden this to Flatten[SortBy[#, Last] & /@ GatherBy[list2, Round[#[[1]]] &], 1] $\endgroup$
    – Bob Hanlon
    Feb 21, 2022 at 15:46

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.