# Sorting Cartesian coordinates

Here is the problem:

I create a coordinate list as follows:

DegreeP = 5;
lst = DeleteDuplicates@
Select[Flatten[
Table[Permutations[{a, b}], {a, 0, DegreeP}, {b, 0, DegreeP}],
2], #[] + #[] <=DegreeP &];


By assigning the index of each coordinate, I can represent the order as a listplot

ListPlot[{lst, lst},
Joined -> {True, False}, PlotStyle -> {Orange, Black},
ImageSize -> Medium, PlotRangeClipping -> False, ImagePadding -> 20,
Epilog -> (n = 1; Text[Style[n++, 11], #, {-1, -1}] & /@ lst)] The real order (solution) in which the coordinates should be found, however, is as follows

sol = {{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}, {1, 1}, {0, 3}, {3, 0}, {1, 2}, {2, 1}, {0, 4},
{4, 0}, {2, 2}, {1, 3}, {3, 1}, {0, 5}, {5, 0}, {2, 3}, {3, 2}, {1, 4}, {4, 1}};


As a ListPlot

ListPlot[{sol, sol},
Joined -> {True, False}, PlotStyle -> {Orange, Black},
ImageSize -> Medium, PlotRangeClipping -> False, ImagePadding -> 20,
Epilog -> (n = 1; Text[Style[n++, 11], #, {-1, -1}] & /@ sol)] I would like to find a way to sort my lst to get the same order as sol

For that, I tried with SortBy[] but I am unable to find the right pattern...

Any idea ?

DegreeP = 9;
lst = Join @@ Map[SortBy[{-Norm@# &}]@FrobeniusSolve[{1, 1}, #] &]@ Range[0, DegreeP];

ListPlot[{lst, lst}, Joined -> {True, False},
PlotStyle -> {Orange, Black}, ImageSize -> 600,
PlotRangeClipping -> False, ImagePadding -> 20,
Epilog -> (n = 1; Text[Style[n++, 11], #, {-1, -1}] & /@ lst)]


Let's print the result for DegreeP={8,9,10}, it seems that there is a "jump" in the pattern for DegreeP = 9. This case does not seem to occur anymore for 20>DegreeP>1.

Note :

• I am only interested in DegreeP<20
• I cannot manually change the position of the elements

DegreeP = 8 DegreeP = 9 DegreeP = 10 Update:

ClearAll[f, shuffle]
shuffle = If[Length @ # <= 2, #,
Join[#[[{1, -1}]],
Take[Riffle[#, Reverse @ #], Length @ #] & @ Delete[#, {{1}, {-1}, {-2}}],
{#[[-2]]}]] &;

f[dp_] := Join @@ Map[shuffle @ FrobeniusSolve[{1, 1}, #] &] @ Range[0, dp];


Examples:

degreeP = 5;

ListPlot[{f[degreeP], f[degreeP]}, Joined -> {True, False},
PlotStyle -> {Orange, Black}, ImageSize -> 600,
PlotRangeClipping -> False, ImagePadding -> 20,
Epilog -> MapIndexed[Text[Style[#2[], 11], #, {-1, -1}]&, f[degreeP]]] With degreeP = 7 we get and degreeP = 9 gives and degreeP = 20 gives degreeP = 5;

sol2 = Join @@ Map[SortBy[{- Norm @ # &}] @
FrobeniusSolve[{1, 1}, #] &] @ Range[0, degreeP];

sol2 == sol

True

ListPlot[{sol2, sol2}, Joined -> {True, False},
PlotStyle -> {Orange, Black}, ImageSize -> Medium,
PlotRangeClipping -> False, ImagePadding -> 20,
Epilog -> MapIndexed[Text[Style[#2[], 11], #, {-1, -1}] &, sol2]] With degreeP = 7 we get • Thanks for your answer, it works well for several DegreeP but there seems to be a problem with the patern for DegreeP = 7. It's a bit out of the scope of my question but do you think we could generalize that for DegreeP = N Mar 3, 2022 at 12:31
• @JocelynMinini, not sure what the correct ordering would be in general. Please try the new version.
– kglr
Mar 3, 2022 at 12:49
• I have updated the question I hope it is more clear Mar 3, 2022 at 13:37
• @JocelynMinini, please see the update.
– kglr
Mar 3, 2022 at 14:33
• It works perfectly. Elegant solution by the way Mar 3, 2022 at 14:56
sol = SortBy[lst, {(First@# + Last@#) &, Norm}]


The above solution was me looking at the display pattern you added to your post.

A little bit more MMA-esque:

sol = SortBy[lst, {Total@# &, Norm}]


and then realizing that Norm has an additional parameter:

sol = SortBy[lst, {Norm[#, 1] &, Norm[#, 2] &}]


OK done.