1

Why is this list returned unchanged by Sort?

Sort[{0,-Pi/2}]

{0,-Pi/2}

While this list is returned reversed?

Sort[{0,-1}]

{-1,0}

Mathematica 11.0.0.0 MacOS

corey979
  • 23,947
  • 7
  • 58
  • 101
Ralph Dratman
  • 1,250
  • 12
  • 20

1 Answers1

1

The Wolfram Documentation says:

Sort by default orders integers, rational, and approximate real numbers by their numerical values.

It also says:

Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth‐first manner.

The phrase -Pi/2 is an expression, so this second definition is applying. i.e. if we convert each expression to 'atomic'/numerical values first...

Sort[{0 // N, -Pi/2 // N}]
> {-1.5708, 0.}

... then it behaves as expected. You can force it to sort by numerical value by explicitly specifying the method used to compare entries for Sorting:

Sort[{0, -Pi/2}, Less]
> {-Pi/2, 0}
Myridium
  • 1,089
  • 5
  • 15