1
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ It is one of common pitfalls (ref). $\endgroup$ – ybeltukov Nov 28 '15 at 21:05
2
$\begingroup$

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  *)
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – bobknight Nov 28 '15 at 20:16
  • $\begingroup$ @bobknight - see edit above. $\endgroup$ – Bob Hanlon Nov 28 '15 at 20:34
  • $\begingroup$ thank for the further explanation, now it's a bit more clear. $\endgroup$ – bobknight Nov 28 '15 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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