I have a list with these elements:
list={1/2 (1 + Sqrt[5]), 1, 1, 1/2 (1 - Sqrt[5]), 0};
I want to sort them with
Sort[list];
I see the bellow result:
Which is incorrect, because N[1/2 (1 - Sqrt[5])]= -0.6
, However I can write Sort[N[list]]
but I need to have the exact numbers, not their approximate values (I mean that I need 1/2 (1 - Sqrt[5])
instead of -0.6
).
Sort[list,N]
to applyN
before sorting (but keeping the original exact values). $\endgroup$Sort[list, N[#1] < N[#2] &]
$\endgroup$Sort
documentation. This is also mentioned in the pitfalls FAQ: UsingSort
incorrectly. $\endgroup$Ordering[]
works. The point ofOrdering[]
is that your create a list of "sort keys" thatSort[]
andOdering[]
can deal with, and you sort that instead of your original list.Ordering[]
will thus tell you in which sequence you should pick the elements of the original list to get the same sequence as the auxiliary list after sorting. $\endgroup$