Sort a list by elements of another listOrdered indices of a multiple product or sumThe efficiency compare between Flatten[#, 1] & and Join @@ # &Lexicographic ordering of lists-of-lists?How-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pairList of (sub-)lists - query sub-lists by names?Can we intelligently control evaluation in Thread?Sort list but keeping the number of swaps requiredFind positions in which list elements are equalHow can I check if elements between lists are equal?comparing lists of strings
How to escape string to filename? It is in backup a file append date
Go Pregnant or Go Home
Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?
Valid Badminton Score?
What are the ramifications of creating a homebrew world without an Astral Plane?
What does this 7 mean above the f flat
What happens if you roll doubles 3 times then land on "Go to jail?"
Is there a good way to store credentials outside of a password manager?
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
What is the best translation for "slot" in the context of multiplayer video games?
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
How does the UK government determine the size of a mandate?
Proof of work - lottery approach
What is the opposite of 'gravitas'?
What is paid subscription needed for in Mortal Kombat 11?
Why is Lord Kartikeya called 'Devasenapati'?
Italian words for tools
Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?
Is this version of a gravity generator feasible?
Anatomically Correct Strange Women In Ponds Distributing Swords
What to do with wrong results in talks?
Type int? vs type int
Why, precisely, is argon used in neutrino experiments?
How did Doctor Strange see the winning outcome in Avengers: Infinity War?
Sort a list by elements of another list
Ordered indices of a multiple product or sumThe efficiency compare between Flatten[#, 1] & and Join @@ # &Lexicographic ordering of lists-of-lists?How-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pairList of (sub-)lists - query sub-lists by names?Can we intelligently control evaluation in Thread?Sort list but keeping the number of swaps requiredFind positions in which list elements are equalHow can I check if elements between lists are equal?comparing lists of strings
$begingroup$
I know there are a plenty of other questions here which appear to be similar, however I did not found anything which could give me a hint.
I have two lists:
list1 = A, 12, B, 10, C, 4; (*ordered according to the second column*)
list2 = B, 5, A, 4, C, 1; (*ordered according to the second column*)
Now I want to sort list2according to the list1-order so the output should be:
(* A, 4, B, 5, C, 1 *)
list-manipulation sorting
$endgroup$
add a comment |
$begingroup$
I know there are a plenty of other questions here which appear to be similar, however I did not found anything which could give me a hint.
I have two lists:
list1 = A, 12, B, 10, C, 4; (*ordered according to the second column*)
list2 = B, 5, A, 4, C, 1; (*ordered according to the second column*)
Now I want to sort list2according to the list1-order so the output should be:
(* A, 4, B, 5, C, 1 *)
list-manipulation sorting
$endgroup$
$begingroup$
to be more specificlist2should be sorted according to the first column oflist1
$endgroup$
– M.A.
2 hours ago
add a comment |
$begingroup$
I know there are a plenty of other questions here which appear to be similar, however I did not found anything which could give me a hint.
I have two lists:
list1 = A, 12, B, 10, C, 4; (*ordered according to the second column*)
list2 = B, 5, A, 4, C, 1; (*ordered according to the second column*)
Now I want to sort list2according to the list1-order so the output should be:
(* A, 4, B, 5, C, 1 *)
list-manipulation sorting
$endgroup$
I know there are a plenty of other questions here which appear to be similar, however I did not found anything which could give me a hint.
I have two lists:
list1 = A, 12, B, 10, C, 4; (*ordered according to the second column*)
list2 = B, 5, A, 4, C, 1; (*ordered according to the second column*)
Now I want to sort list2according to the list1-order so the output should be:
(* A, 4, B, 5, C, 1 *)
list-manipulation sorting
list-manipulation sorting
edited 37 mins ago
MarcoB
37.9k556114
37.9k556114
asked 2 hours ago
M.A.M.A.
645
645
$begingroup$
to be more specificlist2should be sorted according to the first column oflist1
$endgroup$
– M.A.
2 hours ago
add a comment |
$begingroup$
to be more specificlist2should be sorted according to the first column oflist1
$endgroup$
– M.A.
2 hours ago
$begingroup$
to be more specific
list2should be sorted according to the first column of list1$endgroup$
– M.A.
2 hours ago
$begingroup$
to be more specific
list2should be sorted according to the first column of list1$endgroup$
– M.A.
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
list1 = A, 12, B, 10, C, 4, D, 2;
list2 = A, 4, D, 11, B, 5, C, 1;
idx = Lookup[
AssociationThread[list1[[All, 1]] -> Range[Length[list1]]],
list2[[All, 1]]
];
result = list2;
result[[idx]] = list2;
result
A, 4, B, 5, C, 1, D, 11
$endgroup$
add a comment |
$begingroup$
Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]
A, 4, B, 5, C, 1
$endgroup$
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better bePermute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
$endgroup$
– Henrik Schumacher
25 mins ago
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194061%2fsort-a-list-by-elements-of-another-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
list1 = A, 12, B, 10, C, 4, D, 2;
list2 = A, 4, D, 11, B, 5, C, 1;
idx = Lookup[
AssociationThread[list1[[All, 1]] -> Range[Length[list1]]],
list2[[All, 1]]
];
result = list2;
result[[idx]] = list2;
result
A, 4, B, 5, C, 1, D, 11
$endgroup$
add a comment |
$begingroup$
list1 = A, 12, B, 10, C, 4, D, 2;
list2 = A, 4, D, 11, B, 5, C, 1;
idx = Lookup[
AssociationThread[list1[[All, 1]] -> Range[Length[list1]]],
list2[[All, 1]]
];
result = list2;
result[[idx]] = list2;
result
A, 4, B, 5, C, 1, D, 11
$endgroup$
add a comment |
$begingroup$
list1 = A, 12, B, 10, C, 4, D, 2;
list2 = A, 4, D, 11, B, 5, C, 1;
idx = Lookup[
AssociationThread[list1[[All, 1]] -> Range[Length[list1]]],
list2[[All, 1]]
];
result = list2;
result[[idx]] = list2;
result
A, 4, B, 5, C, 1, D, 11
$endgroup$
list1 = A, 12, B, 10, C, 4, D, 2;
list2 = A, 4, D, 11, B, 5, C, 1;
idx = Lookup[
AssociationThread[list1[[All, 1]] -> Range[Length[list1]]],
list2[[All, 1]]
];
result = list2;
result[[idx]] = list2;
result
A, 4, B, 5, C, 1, D, 11
edited 28 mins ago
answered 2 hours ago
Henrik SchumacherHenrik Schumacher
58.1k580160
58.1k580160
add a comment |
add a comment |
$begingroup$
Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]
A, 4, B, 5, C, 1
$endgroup$
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better bePermute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
$endgroup$
– Henrik Schumacher
25 mins ago
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
add a comment |
$begingroup$
Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]
A, 4, B, 5, C, 1
$endgroup$
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better bePermute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
$endgroup$
– Henrik Schumacher
25 mins ago
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
add a comment |
$begingroup$
Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]
A, 4, B, 5, C, 1
$endgroup$
Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]
A, 4, B, 5, C, 1
edited 9 mins ago
answered 52 mins ago
MikeYMikeY
3,503714
3,503714
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better bePermute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
$endgroup$
– Henrik Schumacher
25 mins ago
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
add a comment |
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better bePermute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
$endgroup$
– Henrik Schumacher
25 mins ago
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better be
Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].$endgroup$
– Henrik Schumacher
25 mins ago
$begingroup$
Actually, I like your solution much better than mine. By the way, when I found out that my former solution was incorrect, I also realized that your solution should better be
Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].$endgroup$
– Henrik Schumacher
25 mins ago
1
1
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
8 mins ago
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194061%2fsort-a-list-by-elements-of-another-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
to be more specific
list2should be sorted according to the first column oflist1$endgroup$
– M.A.
2 hours ago