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: [mg124044] Re: SortBy won't order irrationals
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sat, 7 Jan 2012 05:17:44 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201201060916.EAA26862@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

Here's an ordering for irrationals on the second element:

pts = {{0, 2}, {1, Sqrt[2]}, {2, 2}, {4, 1}, {Sqrt[5], 1}, {6,
     1}, {Sqrt[3], 1}, {7, 0}, {-1, 0}};

pts[[Ordering[pts[[All, 2]], All, Less]]]

{{-1, 0}, {7, 0}, {Sqrt[3], 1}, {6, 1}, {Sqrt[5], 1}, {4, 1}, {1,
   Sqrt[2]}, {2, 2}, {0, 2}}

And here's a sort by multiple keys (3rd element then 2nd).

sample data:

threes = RandomChoice[Flatten@pts, {10, 3}]

{{7, 2, -1}, {Sqrt[3], 1, Sqrt[3]}, {2, 0, 0}, {2, 1, 1}, {1, Sqrt[
   5], -1}, {0, 1, 4}, {2, 2, 2}, {1, 1, -1}, {0, 2, Sqrt[5]}, {Sqrt[
   5], 0, 0}}

sort:

Fold[#1[[Ordering[#1[[All, #2]], All, LessEqual]]] &, threes,
  Reverse@{3, 1}]

{{1, Sqrt[5], -1}, {1, 1, -1}, {7, 2, -1}, {2, 0, 0}, {Sqrt[5], 0,
   0}, {2, 1, 1}, {Sqrt[3], 1, Sqrt[3]}, {2, 2, 2}, {0, 2, Sqrt[
   5]}, {0, 1, 4}}

or

Fold[#1[[Ordering[#1[[All, #2]], All, LessEqual]]] &, threes,
  Reverse@{3, 1, 2}]

{{1, 1, -1}, {1, Sqrt[5], -1}, {7, 2, -1}, {2, 0, 0}, {Sqrt[5], 0,
   0}, {2, 1, 1}, {Sqrt[3], 1, Sqrt[3]}, {2, 2, 2}, {0, 2, Sqrt[
   5]}, {0, 1, 4}}

(The sort by 2nd element does nothing in that case.)

The Fold method generalizes. For instance, to order on Sin of 3rd element,  
Abs of 1st, and Cos of 2nd:

Clear[order]
order[list_List, {n_Integer, f_}] :=
  list[[Ordering[f@list[[All, n]], All, LessEqual]]]
sorted = Fold[order, threes, Reverse@{{3, Sin}, {1, Abs}, {2, Cos}}]

{{1, Sqrt[5], -1}, {1, 1, -1}, {7, 2, -1}, {0, 1, 4}, {2, 0,
   0}, {Sqrt[5], 0, 0}, {0, 2, Sqrt[5]}, {2, 1, 1}, {2, 2, 2}, {Sqrt[
   3], 1, Sqrt[3]}}

To see if this worked properly:

sorted /. {x_, y_, z_} :> N@{Sin@z, Abs@x, Cos@y};
% == Sort@%

True

Bobby

On Fri, 06 Jan 2012 03:16:42 -0600, Chris Young <cy56 at comcast.net> 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}}
>
>


-- 
DrMajorBob at yahoo.com



  • Prev by Date: Re: Sorting point-arrays by rows and columns, cont.
  • Next by Date: Re: Extension to BinLists Function
  • Previous by thread: Re: SortBy won't order irrationals
  • Next by thread: Re: SortBy won't order irrationals