RE: Sort into chain

• To: mathgroup at smc.vnet.net
• Subject: [mg45642] RE: [mg45605] Sort into chain
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Fri, 16 Jan 2004 19:57:53 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```
>-----Original Message-----
>From: Goyder Dr HGD [mailto:h.g.d.goyder at cranfield.ac.uk]
To: mathgroup at smc.vnet.net
>Sent: Friday, January 16, 2004 12:05 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg45642] [mg45605] Sort into chain
>
>
>Dear Mathgroup,
>
>How do I sort a list of pairs, for example,
>
>a = {{3, 1}, {2, 6}, {7, 3}, {8, 7}, {10, 8}, {6, 11},
>{12, 10}, {11, 14}, {15, 12}, {14, 18}, {19, 15},
>{18, 20}, {20, 19}, {1, 2}};
>
>
>Into a list where the second entry in each pair is the same as
>the first
>entry in the next pair. Thus the effect of the sort will give
>
>{{18, 20}, {20, 19}, {19, 15}, {15, 12},
>{12, 10}, {10, 8}, {8, 7}, {7, 3}, {3, 1},
>{1, 2}, {2, 6}, {6, 11}, {11, 14}, {14, 18}}
>
>The first pair is not important because the pairs form a
>circular chain.
>
>I have tried
>
>Sort[a, (#1[[2]] == #2[[1]]) &]
>
>but the answer is
>
>{{1, 2}, {20, 19}, {14, 18}, {18, 20}, {19, 15},
>{15, 12}, {6, 11}, {11, 14}, {12, 10}, {10, 8},
>{8, 7}, {7, 3}, {2, 6}, {3, 1}}
>
>which has a few correct entries but is not finished. I wondered if the
>difficulty was due to the circular nature of the chain but if
>you break the
>chain by dropping one pair it still does not work.
>
>Thanks for any assistance
>
>Hugh Goyder
>
>--
>This message has been scanned for viruses and
>dangerous content by the Cranfield MailScanner, and is
>believed to be clean.
>

Using the porperties your data obviously have (from your example):

In[14]:= start = -3;

In[15]:= Block[{s},
Scan[(s[#[[1]]] = #) &, a];
NestList[s[#[[-1]]] &, a[[start]], Length[a] - 1]]
Out[15]=
{{18, 20}, {20, 19}, {19, 15}, {15, 12}, {12, 10}, {10, 8}, {8, 7},
{7, 3}, {3, 1}, {1, 2}, {2, 6}, {6, 11}, {11, 14}, {14, 18}}

Of course you may start from where you want.

--
Hartmut Wolf

```

• Prev by Date: RE: Wolfram Functions Site
• Next by Date: Re: Re: Re: how to delete duplicate items in the same list
• Previous by thread: Re: Sort into chain
• Next by thread: Re: Sort into chain