2
$\begingroup$

When I input

Sort[{-Sqrt[3],0}]

I get

{0,-Sqrt[3]}

This can't be right, can it?

$\endgroup$
3
  • 3
    $\begingroup$ This is expected behaviour. See Possible issues in the Sort documentation: "Numeric expressions are sorted by structure as well as numerical value" $\endgroup$ Nov 19 '16 at 20:39
  • 1
    $\begingroup$ See also mathematica.stackexchange.com/questions/2729/ordering-problem $\endgroup$ Nov 19 '16 at 20:42
  • $\begingroup$ @SimonWoods Thanks for finding the duplicate; it's much more useful to mark this as such than merely closing as easily found in the documentation. $\endgroup$
    – Mr.Wizard
    Nov 20 '16 at 8:18
7
$\begingroup$

This result is correct.

Sort doesn't sort by numerical value. It simply orders expressions—any expression, whether they represent a number of something else.

Use SortBy[..., N] to sort by numerical value.

See the sorting rules under Details in Sort. See also Possible Issues on the same page.

$\endgroup$
1
  • $\begingroup$ Ok, thanks. I was embedding "sort" in a function that calculates a type of signature. My expression works for other numerical expressions (besides this one) and for lists of months, days-of-the-week, letters, words, etc. I can't use /N because I am using sort in this more general context. I can live with this exception. I just needed to see how broadly my function would work. Sort[ { -1.7, 0 } sorts "correctly" so I just thought my subject expression would also. We can close this as I don't require further improvement. $\endgroup$
    – matrixbud
    Nov 20 '16 at 1:23

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