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
your help...
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
With thanks in advance
Yours Gernot
{{a, {2, 4, 1., 8, 1, 4}}, {b, {3.2, -2, -6,
7, -7}}} /.
l_?(VectorQ[#, NumericQ] &) :> Sort[l]
??
Regards
Jens
"Gernot Pfanner" <pfan...@stud.uni-graz.at>
schrieb im Newsbeitrag
news:d6us0f$ivm$1...@smc.vnet.net...
MapAt[Sort, #, 2]&/@expr
{{a, {1., 2, 4}},
{b, {-2, 1, 3.2}}}
expr/.x_?VectorQ:>Sort[x]
{{a, {1., 2, 4}},
{b, {-2, 1, 3.2}}}
Bob Hanlon
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.
> 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
> your help...
> 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
> With thanks in advance
> Yours Gernot
>
In[1]:=
lst = {{a, {2, 4, 1.}},
{b, {3.2, -2}}}
Out[1]=
{{a, {2, 4, 1.}},
{b, {3.2, -2}}}
In[16]:=
({#1[[1]], Sort[
#1[[2]]]} & ) /@ lst
Out[16]=
{{a, {1., 2, 4}},
{b, {-2, 3.2}}}
János
lst = {{a, {2, 4, 1}}, {b, {3.2, -2}}};
lst//. {a___, {b_, c_List /; ! OrderedQ[c, Less]}, d___} :> {a, {b,
Sort[c]}, d}
You can basically set up a transformation rule for any such structured
list.
Sebastjan
In[1]:=
ex={{a,Table[Random[Integer,{0,9}],{5}]},{b,
Table[Random[Integer,{0,9}],{5}]},{c,Table[Random[Integer,{0,9}],{5}]}}
Out[1]=
{{a,{5,3,3,6,0}},{b,{1,4,8,2,2}},{c,{0,0,6,5,5}}}
In[2]:=
Transpose[{#[[1]]&/@ex,Sort/@(#[[2]]&/@ex)}]
Out[2]=
{{a,{0,3,3,5,6}},{b,{1,2,2,4,8}},{c,{0,0,5,5,6}}}
Tomas Garza
Mexico City
>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 your help... 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 With
>thanks in advance Yours Gernot
I would do this as follows:
{First@#, Sort@Last@#}&/@list
where list contains the data you describe.
--
To reply via email subtract one hundred and four
Sincerely, Daniel
Gernot,
in principle there are two complementary methods:
First, dissect your expression, apply Sort to the parts intended and
recompose.
Step by step, let
In[2]:=
l={{a,{2,4,1.,"..."}},{c,{3,1,2,"..."}},{b,{3.2,-2,"..."}}}
In[3]:= {keys, ll} = Transpose[l]
Out[3]=
{{a, c, b}, {{2, 4, 1., "..."}, {3, 1, 2, "..."}, {3.2, -2, "..."}}}
In[4]:= llsorted = Sort /@ ll
Out[4]=
{{1., 2, 4, "..."}, {1, 2, 3, "..."}, {-2, 3.2, "..."}}
In[5]:= Transpose[{keys, llsorted}]
Out[5]=
{{a, {1., 2, 4, "..."}}, {c, {1, 2, 3, "..."}}, {b, {-2, 3.2, "..."}}}
Put together into a one-liner, (e.g., there are lots of variants):
In[7]:= Transpose[{#1, Sort /@ #2}]& @@ Transpose[l]
Out[7]=
{{a, {1., 2, 4, "..."}}, {c, {1, 2, 3, "..."}}, {b, {-2, 3.2, "..."}}}
Second, keep your structure untouched but map Sort to the right parts:
In[10]:= MapAt[Sort, l, Thread[{Range[Length[l]],2}]]
Out[10]=
{{a, {1., 2, 4, "..."}}, {c, {1, 2, 3, "..."}}, {b, {-2, 3.2, "..."}}}
(Of course you may combine both methods appropriately.)
--
Hartmut Wolf
{{a,{2,4,1.,...}}, {c, {1,2,...}},{b,{3.2,-2,...}}}
but I need to sort it based on the first element of each subarray,
while keeping the second element the same.
I did a pretty ugly solution (like 12 lines of code). Is there a
quicker more elegant way to sort this?
In[9]:=
Sort[lst, #2[[2,1]] >=
#1[[2,1]] & ]
Out[9]=
{{c, {1, 2}},
{a, {2, 4, 1.}},
{b, {3.2, -2}}}
János
will give you the sorted list you are looking for:
{{a, {2, 4, 1}}, {b, {3.2, -2}}, {c, {1, 2}}}
Sebastjan
>I have a similar problem, I need to sort the same kind of list
>{{a,{2,4,1.,...}}, {c, {1,2,...}},{b,{3.2,-2,.≠..}}}
>but I need to sort it based on the first element of each subarray,
>while keeping the second element the same.
>I did a pretty ugly solution (like 12 lines of code). Is there a
>quicker more elegant way to sort this?
Is this what you want?
In[8]:=
data = {{a, {2, 4, 1.}}, {c, {1, 2}}, {b, {3.2, -2}}};
Sort[data, OrderedQ[{#1[[2,1]], #2[[2,1]]}] & ]
Out[9]=
{{c, {1, 2}}, {a, {2, 4, 1.}}, {b, {3.2, -2}}}
The simplest idea is to use Sort with an ordering function. An alternate
idea, which is in general much quicker, is to generate a key for each
element in the array, and then use Ordering on this list of keys. In your
case, we would do the following:
In[12]:=
data = {{a, {2, 4, 1.}}, {c, {1, 2}}, {b, {3.2, -2}}};
keys = data[[All, 2, 1]]
Out[13]=
{2, 1, 3.2}
Then, use Ordering to get the sorted list:
In[14]:=
data[[Ordering[keys]]]
Out[14]=
{{c, {1, 2}}, {a, {2, 4, 1.}}, {b, {3.2, -2}}}
Let's compare the timing for this approach versus using Sort with an
ordering function:
data = Table[{Random[], Table[Random[], {10}]}, {10^5}];
In[16]:=
r1 = data[[Ordering[data[[All, 2, 1]]]]]; // Timing
r2 = Sort[data, #2[[2, 1]] > #1[[2, 1]] &]; // Timing
r1 === r2
Out[16]=
{0.157 Second, Null}
Out[17]=
{6.343 Second, Null}
Out[18]=
True
We see that the Ordering approach is about 40 times faster for this data
set.
Carl Woll