Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7620

Re: SortBy won't order irrationals

From DrMajorBob <btreat1@austin.rr.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: SortBy won't order irrationals
Date 2012-01-07 10:19 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <je966t$iu2$1@smc.vnet.net> (permalink)
References <201201060916.EAA26862@smc.vnet.net>

Show all headers | View raw


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@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@yahoo.com

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: SortBy won't order irrationals DrMajorBob <btreat1@austin.rr.com> - 2012-01-07 10:19 +0000

csiph-web