MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: SortBy won't order irrationals

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124052] Re: SortBy won't order irrationals
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 7 Jan 2012 05:20:30 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201201060916.EAA26862@smc.vnet.net>

Numeric symbols like Sqrt[2],Pi,E  etc.  etc are not sorted by their 
value but in the same way as all other symbols. Compare:

Sort[{Pi, 4, Sqrt[2], 2, 3*E}]

{2, 4, Sqrt[2], 3*E, Pi}

with

Sort[{Pi, 4, Sqrt[2], 2, 3*E}, Less]

{Sqrt[2], 2, Pi, 4, 3*E}


Note also that you can use Sort with a custom ordering function.


Andrzej Kozlowski



On 6 Jan 2012, at 10:16, Chris Young wrote:

> Have to use N[ ] on them first. Combined with Sort's refusal to accept
> multiple keys, this is extremely exasperating.
>
> In[1218]:= ptsIrr =
> {
>  {0, 2}, {1, Sqrt[2]}, {2, 2},
>  {4, 1}, {Sqrt[5], 1}, {6, 1}, {Sqrt[3], 1},
>  {7, 0}, {-1, 0}
>  }
>
> Out[1218]= {{0, 2}, {1, Sqrt[2]}, {2, 2}, {4, 1}, {Sqrt[5], 1}, {6,
>  1}, {Sqrt[3], 1}, {7, 0}, {-1, 0}}
>
> In[1219]:= sortIrrY = SortBy[ptsIrr, #[[2]] &]
>
> Out[1219]= {{-1, 0}, {7, 0}, {4, 1}, {6, 1}, {Sqrt[3], 1}, {Sqrt[5],
>  1}, {0, 2}, {2, 2}, {1, Sqrt[2]}}
>
> In[1220]:= sortIrrYN = SortBy[ptsIrr // N, #[[2]] &]
>
> Out[1220]= {{-1., 0.}, {7., 0.}, {1.73205, 1.}, {2.23607, 1.}, {4.,
>  1.}, {6., 1.}, {1., 1.41421}, {0., 2.}, {2., 2.}}
>
> In[1221]:= sortIrrYN2 = SortBy[ptsIrr, N[#[[2]]] &]
>
> Out[1221]= {{-1, 0}, {7, 0}, {4, 1}, {6, 1}, {Sqrt[3], 1}, {Sqrt[5],
>  1}, {1, Sqrt[2]}, {0, 2}, {2, 2}}
>
>




  • Prev by Date: Re: SortBy for multiple key sorts
  • Next by Date: ListPolarPlot questions
  • Previous by thread: SortBy won't order irrationals
  • Next by thread: Re: SortBy won't order irrationals