MathGroup Archive 2004

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

Search the Archive

Re: sort procedure

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52510] Re: [mg52483] sort procedure
  • From: DrBob <drbob at bigfoot.com>
  • Date: Tue, 30 Nov 2004 05:24:24 -0500 (EST)
  • References: <200411290622.BAA27959@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

Here are two methods. I like the second a little better, I think.

data=Array[{#,N@Sin@#}&,{5}];
Sort[data,#1[[2]]<=#2[[2]]&]
data[[Ordering[data[[All,{2,1}]]]]]

{{5,-0.958924},{4,-0.756802},{3,0.14112},{1,0.841471},{2,0.909297}}

{{5,-0.958924},{4,-0.756802},{3,0.14112},{1,0.841471},{2,0.909297}}

If the second element of each pair is exact (Sin@n rather than N@Sin@n, for instance), the Sort isn't numerical by default:

data = Array[{#1, Sin[#1]} & , {5}];
Sort[data, #1[[2]] <= #2[[2]] & ]
data[[Ordering[data[[All,{2, 1}]]]]]

{{5, Sin[5]}, {4, Sin[4]}, {3, Sin[3]}, {1, Sin[1]}, {2, Sin[2]}}
{{1, Sin[1]}, {2, Sin[2]}, {3, Sin[3]}, {4, Sin[4]}, {5, Sin[5]}}

But we can do it any of these three ways:

data = Array[{#1, Sin[#1]} & , {5}];
Sort[data, N[#1[[2]]] <= N[#2[[2]]] & ]
Sort[data, N[#1[[2]] <= #2[[2]]] & ]
data[[Ordering[N[data[[All,{2, 1}]]]]]]

{{5,Sin[5]},{4,Sin[4]},{3,Sin[3]},{1,Sin[1]},{2,Sin[2]}}
{{5,Sin[5]},{4,Sin[4]},{3,Sin[3]},{1,Sin[1]},{2,Sin[2]}}
{{5,Sin[5]},{4,Sin[4]},{3,Sin[3]},{1,Sin[1]},{2,Sin[2]}}

The last method works because, for instance, Sin[1]<=Sin[2] is unevaluated (until N is applied). That's also why the previous Sort didn't change the order.

Bobby

On Mon, 29 Nov 2004 01:22:30 -0500 (EST), paolo <tarpanelli at libero.it> wrote:

> If I have a data serie of n paired observations (x_t,x_t-1) ordered according to time, how can i re-order this serie according to the size of x_t-1
>
> The procedure Sort it seems it does not work well.
> anyone has suggestions?
> thanks
>
> P.
>
>
>
> ____________________________________________________________
> Libero ADSL: navighi gratis a 1.2 Mega, senza canone e costi di attivazione.
> Abbonati subito su http://www.libero.it
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Proving inequalities with Mathematica
  • Next by Date: Re: Changing CellMargins?
  • Previous by thread: Re: sort procedure
  • Next by thread: Re: sort procedure