Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The Sort function will by default sort from lowest to highest.

In[1]:= Sort[{3, 1, 4}]
Out[1]= {1, 3, 4}

However, if the list contains non-numerical values, it will sort it incorrectly

In[2]:= zeroes = {2 Pi - 2 ArcTan[Sqrt[2 + Sqrt[5]]], 
   2 ArcTan[Sqrt[2 + Sqrt[5]]], 2 Pi + 2 ArcTan[Sqrt[2 + Sqrt[5]]]};

In[3]:= zeroes // N
Out[3]= {4.04615, 2.23704, 8.52022}

In[4]:= Sort[zeroes] // N
Out[4]= {4.04615, 2.23704, 8.52022}

One could use the Greater function as a parameter to Sort and then reverse the input

In[5]:= Reverse@Sort[zeroes, Greater] // N
Out[5]= {2.23704, 4.04615, 8.52022}

There is no Lesser function in Mathematica, but there must be a better way to do this, without converting the data to floats.

share|improve this question

closed as off-topic by Michael E2, rasher, Jacob Akkerboom, Sjoerd C. de Vries, bobthechemist Mar 10 at 13:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, rasher, Jacob Akkerboom, Sjoerd C. de Vries, bobthechemist
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This is shown in the last two examples under Scope on the manual page for Sort. –  Michael E2 Mar 10 at 10:20
    
As Michael E2 states, in the documentation. If you're trying to avoid conversion for say performance, makes no difference, when using e.g. Less on expressions, they're evaluated to numeric where possible. –  rasher Mar 10 at 10:37

2 Answers 2

up vote 4 down vote accepted

The function Less is what you can use.

list = {2 Pi - 2 ArcTan[Sqrt[2 + Sqrt[5]]], 
   2 ArcTan[Sqrt[2 + Sqrt[5]]], 2 Pi + 2 ArcTan[Sqrt[2 + Sqrt[5]]]};
Sort[list, Less]

Or,

Sort[list, #1 < #2 &]

Or you could use:

SortBy[list, N@# &]
share|improve this answer
    
+1 for faster typist ;-> –  rasher Mar 10 at 10:14
Sort[zeros// N]
(* {2.23704,4.04615,8.52022} *)

Mathematica is doing exactly what it should. You were sorting the expressions...

Ah, ninja'd... As ubpdqn posted, SortBy if you wish to sort (and keep form) of expressions by numeric value.

share|improve this answer
    
+1 not ninja'd...//N vs ,Less...you 'win' by 2 characters (3 if you count my unnecessary space), am ignoring the first argument :-) –  ubpdqn Mar 10 at 10:15
    
You're not answering the question which specifically asked for a solution without using conversion to floating point precision. –  jVincent Mar 10 at 10:16
    
@jVincent: not sure what your point (if any) is, I state quite clearly use of SortBy as also posted by ubodqn gives the expressions...not to mention the OP is quite ambiguous re: exactly what form of result is desired. –  rasher Mar 10 at 10:19

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