# change order element of a list in a particular order [closed]

I have this list :

{0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}}


I would like to transform it with the following pattern :

{{0,3},0,{0,1},{1,0}, 1, {1,2}, {2,1}, 2, {2,3},{3,2},3,{3,0}}


a) the simple figure j is always between two lists of 2 elements in this way {j,i}, j, {j,k} ; in other terms the 2 lists {j,i} and {j,k} around j starts by j

b) the next list after {j,k} is built with a inversion so after {j,k} we have {k,j}

For example,

For the 0 element, we build : {0,3},0,{0,1}

Next, to go to the 1 element, we use : {1,0}

and the list with 2 elements around 1 are {1,0}, 1, {1,2}

The elements can put in a circular pattern. i will try to add a picture so as to help the understanding.

Would you have some ideas to do that ?

• The two lists are identical. It is not clear what you are trying to accomplish. – Bob Hanlon May 23 at 5:28
• The lists do not even contain the same total number of elements when flattened, so it's not just some "sort" operation. Put down the beer, fix the examples, and if needed describe what exactly the intent is here, so as to preclude answers followed by "well, what I really want to do is...". – ciao May 23 at 6:08
• You need to specify some condition how you want to order the list. – Daniel Huber May 23 at 7:39
• Are you sure about the position of {3,0} and 0? – Lou May 23 at 7:49
• The OP should give the ordering guidelines. Unless that is his assignment and I'm not sure if that is a mathematica question – Lou May 23 at 7:58

We can use RelationGraph + FindPath as follows:

relation = UnsameQ @ ## &&
MatchQ[{##}, {i_, {i_, _}} | {{i_, _}, i_} | {{i_, j_}, {j_, i_}}] &;

l1 = {0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}};

rg = RelationGraph[relation, l1,  PlotTheme -> "NameLabeled", VertexSize -> Large] FindPath[rg, {1, 0}, {0, 1}] // First

{{1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}, {3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}}


Alternatively, you can use relation to construct a list of edges and use it with Graph:

edgelist=UndirectedEdge @@@ Select[relation @@ # &] @ Subsets[l1, {2}];

g = Graph[edgelist, PlotTheme -> "NameLabeled", VertexSize -> Large]


same picture as above

• Thank you it is exactly the résult i wanted. Now would it possible the second list directly with your filter and once you get make a Graph with the Graph fonction. I would prefer a solution without the RelationGraph and especially the FindHamiltonianPath because i need to use it in a code where the Graph function is more used – Bendesarts May 23 at 13:50
• @Bendesarts, I updated with an alternative way to get the same graph without using RelationGraph. You can use FindPath instead of FindHamiltonianPath to get the vertex list in desired order. – kglr May 23 at 14:57

An alternative approach using NestWhile to recursively build the sorted list:

ClearAll[sTep, sOrt]
sTep[list_][{a___, {i_, j_}}] := Module[{p = FirstCase[list, {i, Except[j]}]},
{a, {i, j}, i, p, Reverse @ p}]

sOrt[list_, start_] := Most @ NestWhile[sTep[list], {start}, Length@# < Length@list &]


Examples:

l1 = {0, 1, 2, 3, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 3}, {3, 2}, {0, 3}, {3, 0}};

sOrt[l1, {1, 0}]

  {{1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}, {3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}}


Start with {3, 2} instead of {1, 0}:

sOrt[l1, {3, 2}]

 {{3, 2}, 3, {3, 0}, {0, 3}, 0, {0, 1}, {1, 0}, 1, {1, 2}, {2, 1}, 2, {2, 3}}

• This solution works but it is quite long. – Bendesarts May 24 at 5:37