Re: Sorting nested list

• To: mathgroup at smc.vnet.net
• Subject: [mg57344] Re: Sorting nested list
• From: Jean-Marc Gulliet <jeanmarc.gulliet at 9online.fr>
• Date: Wed, 25 May 2005 06:02:51 -0400 (EDT)
• Organization: New York University
• References: <d6us0f\$ivm\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

Hi Gernot,

I think the following method using Map and anonymous functions should do
the work:

In[1]:=
data={{b,{2,4,1.0}},{a,{3.2,-2,5,-3}}};

In[2]:=
Map[{#[[1]],Sort[#[[2]]]}&,data]

Out[2]=
{{b,{1.,2,4}},{a,{-3,-2,3.2,5}}}

Basically, the above expression tells Mathematica to go through the list
called "data" element by element, that is pair by pair in this case, and
to apply the anonymous function {#[[1]],Sort[#[[2]]]}& on each of them.
This function creates a new pair where the first element #[[1]] is not
changed and the second #[[2]] is sorted with the default option of the
command Sort.

Best regards,
/J.M.

Gernot Pfanner wrote:
> Hi!
>
> Please forgive me, if this is the 100.000th posting concerning sorting a
> list. But at the moment I'm pretty confused, and so I ask you kindly for
> Given something like
> {{a,{2,4,1.,...}},{b,{3.2,-2,...}}}
> How do I sort just the inner lists (i.e. e.g. {2,4,1.,...}), so that my
> object finally looks like
> {{a,{1.,2,4.,...}},{b,{-2,3.2,...}}}
> In this spirit
> Yours Gernot
>

• Prev by Date: Re: Sorting nested list
• Next by Date: Re: Log function
• Previous by thread: Re: Sorting nested list
• Next by thread: Re: Sorting nested list