# Unexpected result from Sort with symbolic values

I think Sort should be able to sort symbolic values that are reducible to numerical values, but this example seems to get a wrong answer (here is a simplified version of a more complex array of value):

data = {1/(
2 Log[8]) ((3206499 Log[33554432/3206499])/33554432 + (
2690241 Log[33554432/2690241])/33554432), ((
177251 Log[1048576/177251])/1048576 + (84893 Log[1048576/84893])/
1048576)^2/(2 Log[8]), ((51529 Log[524288/51529])/262144) /(2
Log[8]),  ((42113 Log[262144/42113])/131072 + (
38795 Log[262144/38795])/131072 + (32705 Log[262144/32705])/
131072 + (17459 Log[262144/17459])/131072)/(2 Log[8]) };

N @ Sort @ data
(* {0.109649, 0.0610795, 0.1026, 0.488881} *)

Sort @ N @ data

(* {0.0610795, 0.1026, 0.109649, 0.488881} *)


Is there any mistake I'm doing?

• It is one of common pitfalls (ref). – ybeltukov Nov 28 '15 at 21:05

START EDIT: "Sort by default orders integers, rational, and approximate real numbers by their numerical values." Since your expressions are none of these, you should expect the "canonical order" to likely be other than numeric. "In most cases, NumericQ[expr] gives True whenever N[expr] yields an explicit number"; consequently, NumericQ will return True for more than integers, rational, and approximate real numbers.

Your example data are not integers, rational, or approximate real numbers but rather expressions containing symbolic elements. "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner." Use SortBy

data = {1/(2 Log[8]) ((3206499 Log[33554432/3206499])/
33554432 + (2690241 Log[33554432/2690241])/
33554432), ((177251 Log[1048576/177251])/
1048576 + (84893 Log[1048576/84893])/1048576)^2/(2 Log[
8]), ((51529 Log[524288/51529])/
262144)/(2 Log[8]), ((42113 Log[262144/42113])/
131072 + (38795 Log[262144/38795])/131072 + (32705 Log[262144/32705])/
131072 + (17459 Log[262144/17459])/131072)/(2 Log[8])};

SortBy[data, N]

(*  {((177251*Log[1048576/177251])/
1048576 +
(84893*Log[1048576/84893])/
1048576)^2/(2*Log[8]),
((3206499*Log[33554432/3206499])/
33554432 +
(2690241*Log[33554432/2690241])/
33554432)/(2*Log[8]),
(51529*Log[524288/51529])/
(524288*Log[8]), (1/(2*Log[8]))*
((42113*Log[262144/42113])/
131072 +
(38795*Log[262144/38795])/
131072 +
(32705*Log[262144/32705])/
131072 +
(17459*Log[262144/17459])/
131072)}  *)

% // N

(*  {0.0610795, 0.1026, 0.109649, 0.488881}  *)

(N@SortBy[data, N]) === (Sort@N@data)

(*  True  *)


Also,

SortBy[data, N] === SortBy[N][data]

(*  True  *)

• thanks for the answer, I was aware of the SortBy (by N) or even Sort@N@data. My wonder was about why the first result is not correct. The sentence you posted is not fully clear to me, I've read it in the Sort reference page but cannot fully understand what it means. I thought that Sort automatically works using numerical value of integer, rational and numerical epxression (e.g. when NumericQ is True), but it's not that way, I need somehow to convert expressions to numbers. – bobknight Nov 28 '15 at 20:16
• @bobknight - see edit above. – Bob Hanlon Nov 28 '15 at 20:34
• thank for the further explanation, now it's a bit more clear. – bobknight Nov 28 '15 at 20:39