2

When I input

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

I get

{0,-Sqrt[3]}

This can't be right, can it?

corey979
  • 23,947
  • 7
  • 58
  • 101
matrixbud
  • 435
  • 2
  • 9
  • 3
    This is expected behaviour. See Possible issues in the Sort documentation: "Numeric expressions are sorted by structure as well as numerical value" – Simon Woods Nov 19 '16 at 20:39
  • 1
    See also http://mathematica.stackexchange.com/questions/2729/ordering-problem – Simon Woods Nov 19 '16 at 20:42
  • @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. – Mr.Wizard Nov 20 '16 at 08:18

1 Answers1

7

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 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. – matrixbud Nov 20 '16 at 01:23