Updated to handle OP's MWE.
Another approach is to iterate through the master list and locate the positions in the randomly ordered list where positions 3 through 6 occur.
I will use the OP example data (see question) for MasterPositionList
and list
(i.e, the random order list).
Locate the rows in the MasterPositionsList
where columns three through six match those same columns in list
.
Note that for the example data there are only three matches; there is not a one to one correspondence between the two lists.
listToMaster =
Flatten[Map[Position[list[[All, 3 ;; 6]], #[[3 ;; 6]]] &,
MasterPositionsList], 2]
(* {5, 23, 6} *)
Next we extract those three rows from list
. They are shuffled to be in the same order as the MasterPositionsList
.
list[[#]] & /@ listToMaster
(* {{5, "A5", 100, 100, 100, 0, 0.0221, 0.0234, 0.0248, 0.0255,
0.0253, 0.0245, 0.0245, 0.0245, 0.0251, 0.0301, 0.0421, 0.0523,
0.0518, 0.0423, 0.0309, 0.0254, 0.0243, 0.0235, 0.0225, 0.0235,
0.0266, 0.0306, 0.0329, 0.0335, 0.0342, 0.035, 0.0361, 0.0387,
0.0422, 0.0438, 0.0426, 0.0403, 0.0377, 0.0374, 0.0412, 0.0506}, {1,
"A1", 0, 100, 100, 0, 0.0298, 0.0287, 0.0265, 0.0276, 0.027,
0.0262, 0.0261, 0.0257, 0.0271, 0.0325, 0.0447, 0.0561, 0.0569,
0.0479, 0.0367, 0.0319, 0.0325, 0.0338, 0.0348, 0.045, 0.0902,
0.2007, 0.3558, 0.5152, 0.6334, 0.7072, 0.7548, 0.7836, 0.7985,
0.8008, 0.797, 0.7961, 0.8023, 0.8124, 0.8208, 0.829}, {2, "A2",
100, 0, 100, 0, 0.0235, 0.0242, 0.0255, 0.0262, 0.0267, 0.0263,
0.0271, 0.0276, 0.0306, 0.0462, 0.1085, 0.2178, 0.306, 0.3336,
0.3086, 0.2558, 0.1985, 0.1435, 0.0962, 0.0655, 0.0499, 0.0428,
0.0388, 0.0367, 0.0367, 0.0374, 0.0388, 0.0415, 0.0453, 0.047,
0.0458, 0.0435, 0.0408, 0.0407, 0.045, 0.0545}} *)
The three rows that match are 100, 100, 100, 0
and 0, 100, 100, 0
and finally 0, 100, 0, 100
.
Nearest
If it is desired to re-shuffle list
so that the order is as close as possible to columns three through six of the MasterPositionList
the following approach using Nearest
should work.
Step 1
Iterate through list
and extract the elements from MasterPositionList
that are closest to columns 3-6.
In some cases, multiple elements fit so take the first one.
masterElements =
Map[First[#] &,
Map[Nearest[MasterPositionsList[[All, 3 ;; 6]], #[[3 ;; 6]]] &, list]
]
(* {{0, 0, 0, 0}, {0, 0, 0, 0}, {100, 100, 100, 0}, {100, 100,
100, 0}, {100, 100, 100, 0}, {100, 0, 100, 0}, {95, 0, 0, 0}, {95,
0, 95, 0}, {100, 100, 100, 0}, {100, 100, 100, 0}, {100, 100, 100,
0}, {100, 100, 100, 0}, {70, 0, 70, 0}, {100, 100, 100, 0}, {0, 50,
50, 0}, {50, 0, 50, 0}, {40, 0, 40, 0}, {40, 0, 40, 0}, {20, 0, 20,
0}, {20, 0, 20, 0}, {10, 0, 10, 0}, {10, 0, 10, 0}, {0, 100, 100,
0}, {0, 95, 95, 0}, {0, 90, 90, 0}} *)
Step 2
Iterate through masterElements
and locate the row in MasterPositionList
where those elements can be found.
masterOrder =
Flatten[Map[Position[MasterPositionsList[[All, 3 ;; 6]], #] &,
masterElements], 2]
(* {26, 26, 1, 1, 1, 50, 12, 49, 1, 1, 1, 1, 46, 1, 19, 44, 43, \
43, 41, 41, 40, 40, 25, 24, 23} *)
Below the left column is the index in list
and the right column is the index in MasterPositionList
that most closely matches columns 3-6.

Step 3
Now we need to sort masterOrder
from lowest to highest.
newOrderIndex = Ordering[masterOrder]
(* {3, 4, 5, 9, 10, 11, 12, 14, 7, 15, 25, 24, 23, 1, 2, 21,
22, 19, 20, 17, 18, 16, 13, 8, 6} *)
Step 4
Create listOrdered
from list
using newOrderIndex
.
listOrdered =
Table[list[[newOrderIndex[[i]]]], {i, Range[Length[list]]}]
Step 5
Validate the results.
Below the left hand side are columns 3-6 of MasterPositionList
and the right hand side are the columns 3-6 of listOrdered
. An inspection shows that it is in the correct order.

newindx
produces an error. Looks like you left out part of the table. $\endgroup$file
which is undefined. $\endgroup$