# What is the best way to rearrange the following list? [duplicate]

I have the following list

\[ScriptL]0 = 4
mtablep = Riffle[Range[0, -\[ScriptL]0, -1], Range[\[ScriptL]0]]


which produces the following:

 {0, 1, -1, 2, -2, 3, -3, 4, -4}


I want to have my list as:

 {0, 1, -1, -2, 2, 3, -3, -4, 4}


Could you help me with this?

• Does this answer your question? Move element inside a list May 28 at 16:25
• Two brief demonstrations from the previous link: (mtablep // moveElementUp[#, 4, 5] &) // moveElementUp[#, 8, 9] & and also moveElement[moveElement[mtablep, 4, 5], 8, 9] give as output {0, 1, -1, -2, 2, 3, -3, -4, 4} May 28 at 16:25
• Can you give a rule for your order? May 28 at 19:34

If, for some reason, you can't generate them in the correct order as @qbg suggested, you could always use Sort with a custom sorting function afterwards.

\[ScriptL]0 = 4;
mtablep = Riffle[Range[0, -\[ScriptL]0, -1], Range[\[ScriptL]0]];
sortfunc = Which[
Abs[#2] == Abs[#1] \[And] EvenQ[#1],
RealSign[#2 - #1],
Abs[#2] == Abs[#1] \[And] OddQ[#1],
RealSign[#1 - #2],
True,
RealSign[Abs[#2] - Abs[#1]]
] &;
Sort[mtablep, sortfunc]


{0,1,-1,-2,2,3,-3,-4,4}

Basically, this checks to see if the absolute values are the same. If they are, put the negative one first if it's even and second if it's odd (at least I presume that's the rule you're using). If the absolute values are not the same, then sort by absolute value.

Maybe you can just generate the list in the right order to start with? Assuming this is your rule:

 In[1]:= \[ScriptL]0 = 4;
In[2]:= Flatten@{0, Table[If[Mod[x, 2] == 0, -1, 1]*{x, -x}, {x, \[ScriptL]0}]}
Out[2]:= {0, 1, -1, -2, 2, 3, -3, -4, 4}