RE: Sort by buried element in list

• To: mathgroup at smc.vnet.net
• Subject: [mg48924] RE: [mg48899] Sort by buried element in list
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Thu, 24 Jun 2004 05:35:40 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```>-----Original Message-----
>From: Steve Gray [mailto:stevebg at adelphia.net]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, June 22, 2004 11:32 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg48924] [mg48899] Sort by buried element in list
>
>
>	What's the best way to sort a multilevel list of the following
>type, using as the key the entries 065794, 205347, 000000, 004354,
>etc.? Of course I need all sublists sorted with the key. The entire
>list may be several thousand entries long. Speed is not much of a
>concern. The entries are computed one at a time, and the sort could be
>done at that time or all at once later. I need to (optionally) remove
>entries with duplicate keys, regardless of the other items in the
>entry. The position of the key is known beforehand (obviously).
>
>{{65,98},{44,66},{48,69},{12,54},{98,59},{33,75}},{065794,{0,1,
>0,1,0,2}}
>{{12,84},(04,79},{92,52},{01,71},{49,80},{96,44}},{205347,{0,3,
>2,2,2,3}}
>{{83,81},{52,77},{17,29},{33,13},{49,10},{78,78}},{000000,{0,0,
>0,0,0,0}}
>{{38,25},{74,97},{18,32},{02,08},{43,71},{04,27}},{004354,{0,0,
>1,1,0,2}}
>
>	The final result for this small sample would be
>
>{{83,81},{52,77},{17,29},{33,13},{49,10},{78,78}},{000000,{0,0,
>0,0,0,0}}
>{{38,25},{74,97},{18,32},{02,08},{43,71},{04,27}},{004354,{0,0,
>1,1,0,2}}
>{{65,98},{44,66},{48,69},{12,54},{98,59},{33,75}},{065794,{0,1,
>0,1,0,2}}
>{{12,84},(04,79},{92,52},{01,71},{49,80},{96,44}},{205347,{0,3,
>2,2,2,3}}
>
>	Thanks for any info.
>
>Steve Gray
>
>
>

Steve,
be this your list:

In[1]:= ll =
{{{{65,98},{44,66},{48,69},{12,54},{98,59},{33,75}},{065794,{0,1,0,1,0,2}}},
{{{12,84},{104,79},{92,52},{01,71},{49,80},{96,44}},{205347,{0,3,2,2,2,3}}},
{{{83,81},{52,77},{17,29},{33,13},{49,10},{78,78}},{000000,{0,0,0,0,0,0}}},
{{{38,25},{74,97},{18,32},{02,08},{43,71},{04,27}},{004354,{0,0,1,1,0,2}}}};

Describe keyPos as the position specification (this is a sequence) of the key in each record:

In[2]:= keyPos = Sequence[-1,1];

In[3]:= Part[ll[[2]], keyPos]
Out[3]= 205347

This gives the keys of both, e.g. the second and third records:

In[4]:= Part[ll[[{2,3}]],{1,2}, keyPos]
Out[4]= {205347,0}

We pull out the expression for the List of the two records, by use of Function application:

In[5]:= Part[#,{1,2},-1,1]&[ll[[{2,3}]]]
Out[5]= {205347,0}

Transfering the (outer) List back to Part

In[6]:= Part[{##}, {1, 2}, keyPos] &[ll[[2]], ll[[3]]]
Out[6]= {205347,0}

Now applying OrderedQ gives the ordering function wanted for Sort

In[8]:= OrderedQ[Part[{##}, {1, 2}, keyPos]] &[ll[[2]], ll[[3]]]
Out[8]= False

In[9]:= Sort[ll, OrderedQ[Part[{##}, {1, 2}, keyPos]] &]
Out[9]=
{{{{83,81},{52,77},{17,29},{33,13},{49,10},{78,78}},{0,{0,0,0,0,0,0}}},
{{{38,25},{74,97},{18,32},{2,8},{43,71},{4,27}},{4354,{0,0,1,1,0,2}}},
{{{65,98},{44,66},{48,69},{12,54},{98,59},{33,75}},{65794,{0,1,0,1,0,2}}},
{{{12,84},{104,79},{92,52},{1,71},{49,80},{96,44}},{205347,{0,3,2,2,2,3}}}}

For a different application just specify the appropriate keyPos.

--
Hartmut Wolf

```

• Prev by Date: RE: Re: faster sublist checking
• Next by Date: Seeking a compact expression in a model.
• Previous by thread: Re: Sort by buried element in list
• Next by thread: coupled eqns in NDSolve