You can get newdnames1
directly using
dnames1[[Ordering[dnumber]]]
If you you want to work with associations, for newdnumber
, you can get the desired result simply using Sort
:
assoc1 = AssociationThread[dnames1, dnumber]
<|"A1" -> 20, "A2" -> 20, "A3" -> 11, "A4" -> 21, "A5" -> 19, "A6" -> 20|>
You can get newdnames1
directly using
dnames1[[Ordering[dnumber]]]
If you you want to work with associations, for newdnumber
, you can get the desired result simply using Sort
:
assoc2b = Sort[assoc1]
<|"A3" -> 11, "A5" -> 19, "A1" -> 20, "A2" -> 20, "A6" -> 20, "A4" -> 21|>
Keys @ assoc2
{"A3", "A5", "A1", "A2", "A6", "A4"}
For general newdnumber
, you can sort assoc1
using SortBy[PositionIndex[newdnumber]]
:
assoc2 = SortBy[PositionIndex[newdnumber]]@assoc1
<|"A3" -> 11, "A5" -> 19, "A4" -> 21, "A1" -> 20, "A2" -> 20, "A6" -> 20|>
Keys @ assoc2
{"A3", "A5", "A1", "A2", "A6", "A4"}
SeedRandom[1]
shuffledDnumber = RandomSample[dnumber]
{19, 11, 20, 20, 21, 20}
assoc3 = SortBy[PositionIndex[shuffledDnumber]]@assoc1
<|"A5" -> 19, "A3" -> 11, "A4" -> 21, "A1" -> 20, "A2" -> 20, "A6" -> 20|>
Keys @ assoc3
{"A5", "A3", "A4", "A1", "A2", "A6"}