Re: Sort into chain

• To: mathgroup at smc.vnet.net
• Subject: [mg45660] Re: Sort into chain
• From: bobhanlon at aol.com (Bob Hanlon)
• Date: Sat, 17 Jan 2004 02:34:29 -0500 (EST)
• References: <bu8ia8\$aqj\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Use ReplaceRepeated:

a //. {s___, {x_, y_}, m__, {y_, z_}, e___} :>
{s, {x, y}, {y, z}, m, e}

Bob Hanlon

In article <bu8ia8\$aqj\$1 at smc.vnet.net>, Goyder Dr HGD
<h.g.d.goyder at cranfield.ac.uk> wrote:

<< 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]]) &]

{{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.

```

• Prev by Date: Re: Who find a better fit of this experimental data?
• Next by Date: Re: using functions with package name prefixed.
• Previous by thread: RE: Sort into chain
• Next by thread: Who find a better fit of this experimental data?