Sort a list by elements of another listOrdered indices of a multiple product or sumImporting, sorting and exporting listsThe efficiency compare between Flatten[#, 1] & and Join @@ # &Lexicographic ordering of lists-of-lists?Problem with Custom Sort/Split/GatherApplying multiple functions to a single column in a tableList of (sub-)lists - query sub-lists by names?Find positions in which list elements are equalHow can I check if elements between lists are equal?comparing lists of strings

Do sharpies or markers damage soft rock climbing gear?

Why is explainability not one of the criteria for publication?

Why is there not a willingness from the world to step in between Pakistan and India?

Why was this commercial plane highly delayed mid-flight?

Half filled water bottle

What is the meaning/usefulness of this exchange in Seinfeld's The Limo?

Will removing shelving screws from studs damage the studs?

Is the internet in Madagascar faster than in UK?

How to prevent a hosting company from accessing a VM's encryption keys?

Can I use coax outlets for cable modem?

Time difference between banns and marriage

Alternatives to Network Backup

Can I get a PhD for developing an educational software?

Can someone identify this unusual plane at airport?

Can I create something like a macro in Numbers?

What was the point of "Substance"?

Book featuring a child learning from a crowdsourced AI book

Can I take a boxed bicycle on a German train?

Is there any problem with a full installation on a USB drive?

How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?

Why does the `ls` command sort files like this?

Group riding etiquette

Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?

Federal Pacific 200a main panel problem with oversized 100a 2pole breaker



Sort a list by elements of another list


Ordered indices of a multiple product or sumImporting, sorting and exporting listsThe efficiency compare between Flatten[#, 1] & and Join @@ # &Lexicographic ordering of lists-of-lists?Problem with Custom Sort/Split/GatherApplying multiple functions to a single column in a tableList of (sub-)lists - query sub-lists by names?Find positions in which list elements are equalHow can I check if elements between lists are equal?comparing lists of strings






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








8












$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 *)









share|improve this question











$endgroup$













  • $begingroup$
    to be more specific list2should be sorted according to the first column of list1
    $endgroup$
    – M.A.
    Mar 27 at 18:58






  • 1




    $begingroup$
    Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
    $endgroup$
    – Jason B.
    Mar 27 at 23:24






  • 4




    $begingroup$
    list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
    $endgroup$
    – Daniel Lichtblau
    Mar 27 at 23:37

















8












$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 *)









share|improve this question











$endgroup$













  • $begingroup$
    to be more specific list2should be sorted according to the first column of list1
    $endgroup$
    – M.A.
    Mar 27 at 18:58






  • 1




    $begingroup$
    Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
    $endgroup$
    – Jason B.
    Mar 27 at 23:24






  • 4




    $begingroup$
    list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
    $endgroup$
    – Daniel Lichtblau
    Mar 27 at 23:37













8












8








8


1



$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 *)









share|improve this question











$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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 20:35









MarcoB

39.1k5 gold badges59 silver badges121 bronze badges




39.1k5 gold badges59 silver badges121 bronze badges










asked Mar 27 at 18:56









M.A.M.A.

1037 bronze badges




1037 bronze badges














  • $begingroup$
    to be more specific list2should be sorted according to the first column of list1
    $endgroup$
    – M.A.
    Mar 27 at 18:58






  • 1




    $begingroup$
    Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
    $endgroup$
    – Jason B.
    Mar 27 at 23:24






  • 4




    $begingroup$
    list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
    $endgroup$
    – Daniel Lichtblau
    Mar 27 at 23:37
















  • $begingroup$
    to be more specific list2should be sorted according to the first column of list1
    $endgroup$
    – M.A.
    Mar 27 at 18:58






  • 1




    $begingroup$
    Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
    $endgroup$
    – Jason B.
    Mar 27 at 23:24






  • 4




    $begingroup$
    list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
    $endgroup$
    – Daniel Lichtblau
    Mar 27 at 23:37















$begingroup$
to be more specific list2should be sorted according to the first column of list1
$endgroup$
– M.A.
Mar 27 at 18:58




$begingroup$
to be more specific list2should be sorted according to the first column of list1
$endgroup$
– M.A.
Mar 27 at 18:58




1




1




$begingroup$
Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
$endgroup$
– Jason B.
Mar 27 at 23:24




$begingroup$
Is there a better example to show what you want? Doesn't Sort[list2] give the desired output?
$endgroup$
– Jason B.
Mar 27 at 23:24




4




4




$begingroup$
list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
$endgroup$
– Daniel Lichtblau
Mar 27 at 23:37




$begingroup$
list2[[OrderingBy[list1, -#[[2]] &]]] in the next release...
$endgroup$
– Daniel Lichtblau
Mar 27 at 23:37










3 Answers
3






active

oldest

votes


















7













$begingroup$

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]



A, 4, B, 5, C, 1







share|improve this answer











$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 be Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
    $endgroup$
    – Henrik Schumacher
    Mar 27 at 20:47






  • 1




    $begingroup$
    Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
    $endgroup$
    – MikeY
    Mar 27 at 21:04


















6













$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







share|improve this answer











$endgroup$














  • $begingroup$
    works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
    $endgroup$
    – M.A.
    Mar 27 at 21:29


















5













$begingroup$

ugly but fast:



list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]



A, 4, B, 5, C, 1




even faster:



result = list2;
result[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
result



A, 4, B, 5, C, 1




benchmarks



s = 10^7;
list1 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];
list2 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];

(* my first solution *)
result1 = list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]; //AbsoluteTiming//First
(* 8.6416 *)

(* my second solution *)
result2 = Module[L,
L = list2;
L[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
L]; //AbsoluteTiming//First
(* 6.89593 *)

(* MikeY's solution *)
result3 = Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]]; //AbsoluteTiming//First
(* 15.808 *)

(* Henrik Schumacher's solution *)
result4 = Module[idx, L,
idx = Lookup[AssociationThread[list1[[All, 1]] -> Range[Length[list1]]], list2[[All, 1]]];
L = list2;
L[[idx]] = list2;
L]; //AbsoluteTiming//First
(* 31.7412 *)

(* make sure all methods agree *)
result1 == result2 == result3 == result4
(* True *)





share|improve this answer











$endgroup$














  • $begingroup$
    Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
    $endgroup$
    – MikeY
    Mar 27 at 23:34













Your Answer








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
);



);













draft saved

draft discarded


















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

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









7













$begingroup$

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]



A, 4, B, 5, C, 1







share|improve this answer











$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 be Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
    $endgroup$
    – Henrik Schumacher
    Mar 27 at 20:47






  • 1




    $begingroup$
    Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
    $endgroup$
    – MikeY
    Mar 27 at 21:04















7













$begingroup$

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]



A, 4, B, 5, C, 1







share|improve this answer











$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 be Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]].
    $endgroup$
    – Henrik Schumacher
    Mar 27 at 20:47






  • 1




    $begingroup$
    Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
    $endgroup$
    – MikeY
    Mar 27 at 21:04













7














7










7







$begingroup$

Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]



A, 4, B, 5, C, 1







share|improve this answer











$endgroup$



Permute[list2, FindPermutation[ list2[[All,1]] , list1[[All,1]] ] ]



A, 4, B, 5, C, 1








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 21:03

























answered Mar 27 at 20:21









MikeYMikeY

4,1789 silver badges16 bronze badges




4,1789 silver badges16 bronze badges














  • $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
    Mar 27 at 20:47






  • 1




    $begingroup$
    Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
    $endgroup$
    – MikeY
    Mar 27 at 21:04
















  • $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
    Mar 27 at 20:47






  • 1




    $begingroup$
    Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
    $endgroup$
    – MikeY
    Mar 27 at 21:04















$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
Mar 27 at 20:47




$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
Mar 27 at 20:47




1




1




$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
Mar 27 at 21:04




$begingroup$
Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
$endgroup$
– MikeY
Mar 27 at 21:04













6













$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







share|improve this answer











$endgroup$














  • $begingroup$
    works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
    $endgroup$
    – M.A.
    Mar 27 at 21:29















6













$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







share|improve this answer











$endgroup$














  • $begingroup$
    works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
    $endgroup$
    – M.A.
    Mar 27 at 21:29













6














6










6







$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







share|improve this answer











$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








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 27 at 20:44

























answered Mar 27 at 19:09









Henrik SchumacherHenrik Schumacher

67.8k5 gold badges96 silver badges187 bronze badges




67.8k5 gold badges96 silver badges187 bronze badges














  • $begingroup$
    works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
    $endgroup$
    – M.A.
    Mar 27 at 21:29
















  • $begingroup$
    works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
    $endgroup$
    – M.A.
    Mar 27 at 21:29















$begingroup$
works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
$endgroup$
– M.A.
Mar 27 at 21:29




$begingroup$
works well with the example lists. However, something goes wrong when I use other lists with Strings in the first columns instead of A, Band C....
$endgroup$
– M.A.
Mar 27 at 21:29











5













$begingroup$

ugly but fast:



list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]



A, 4, B, 5, C, 1




even faster:



result = list2;
result[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
result



A, 4, B, 5, C, 1




benchmarks



s = 10^7;
list1 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];
list2 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];

(* my first solution *)
result1 = list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]; //AbsoluteTiming//First
(* 8.6416 *)

(* my second solution *)
result2 = Module[L,
L = list2;
L[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
L]; //AbsoluteTiming//First
(* 6.89593 *)

(* MikeY's solution *)
result3 = Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]]; //AbsoluteTiming//First
(* 15.808 *)

(* Henrik Schumacher's solution *)
result4 = Module[idx, L,
idx = Lookup[AssociationThread[list1[[All, 1]] -> Range[Length[list1]]], list2[[All, 1]]];
L = list2;
L[[idx]] = list2;
L]; //AbsoluteTiming//First
(* 31.7412 *)

(* make sure all methods agree *)
result1 == result2 == result3 == result4
(* True *)





share|improve this answer











$endgroup$














  • $begingroup$
    Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
    $endgroup$
    – MikeY
    Mar 27 at 23:34















5













$begingroup$

ugly but fast:



list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]



A, 4, B, 5, C, 1




even faster:



result = list2;
result[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
result



A, 4, B, 5, C, 1




benchmarks



s = 10^7;
list1 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];
list2 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];

(* my first solution *)
result1 = list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]; //AbsoluteTiming//First
(* 8.6416 *)

(* my second solution *)
result2 = Module[L,
L = list2;
L[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
L]; //AbsoluteTiming//First
(* 6.89593 *)

(* MikeY's solution *)
result3 = Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]]; //AbsoluteTiming//First
(* 15.808 *)

(* Henrik Schumacher's solution *)
result4 = Module[idx, L,
idx = Lookup[AssociationThread[list1[[All, 1]] -> Range[Length[list1]]], list2[[All, 1]]];
L = list2;
L[[idx]] = list2;
L]; //AbsoluteTiming//First
(* 31.7412 *)

(* make sure all methods agree *)
result1 == result2 == result3 == result4
(* True *)





share|improve this answer











$endgroup$














  • $begingroup$
    Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
    $endgroup$
    – MikeY
    Mar 27 at 23:34













5














5










5







$begingroup$

ugly but fast:



list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]



A, 4, B, 5, C, 1




even faster:



result = list2;
result[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
result



A, 4, B, 5, C, 1




benchmarks



s = 10^7;
list1 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];
list2 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];

(* my first solution *)
result1 = list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]; //AbsoluteTiming//First
(* 8.6416 *)

(* my second solution *)
result2 = Module[L,
L = list2;
L[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
L]; //AbsoluteTiming//First
(* 6.89593 *)

(* MikeY's solution *)
result3 = Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]]; //AbsoluteTiming//First
(* 15.808 *)

(* Henrik Schumacher's solution *)
result4 = Module[idx, L,
idx = Lookup[AssociationThread[list1[[All, 1]] -> Range[Length[list1]]], list2[[All, 1]]];
L = list2;
L[[idx]] = list2;
L]; //AbsoluteTiming//First
(* 31.7412 *)

(* make sure all methods agree *)
result1 == result2 == result3 == result4
(* True *)





share|improve this answer











$endgroup$



ugly but fast:



list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]



A, 4, B, 5, C, 1




even faster:



result = list2;
result[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
result



A, 4, B, 5, C, 1




benchmarks



s = 10^7;
list1 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];
list2 = Transpose[PermutationList@RandomPermutation[s],
RandomInteger[0, 10, s]];

(* my first solution *)
result1 = list2[[Ordering[list2[[All, 1]]][[Ordering[Ordering[list1[[All, 1]]]]]]]]; //AbsoluteTiming//First
(* 8.6416 *)

(* my second solution *)
result2 = Module[L,
L = list2;
L[[Ordering[list1[[All, 1]]]]] = SortBy[list2, First];
L]; //AbsoluteTiming//First
(* 6.89593 *)

(* MikeY's solution *)
result3 = Permute[list2, FindPermutation[list2[[All, 1]], list1[[All, 1]]]]; //AbsoluteTiming//First
(* 15.808 *)

(* Henrik Schumacher's solution *)
result4 = Module[idx, L,
idx = Lookup[AssociationThread[list1[[All, 1]] -> Range[Length[list1]]], list2[[All, 1]]];
L = list2;
L[[idx]] = list2;
L]; //AbsoluteTiming//First
(* 31.7412 *)

(* make sure all methods agree *)
result1 == result2 == result3 == result4
(* True *)






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 12:24

























answered Mar 27 at 21:18









RomanRoman

15.8k1 gold badge21 silver badges53 bronze badges




15.8k1 gold badge21 silver badges53 bronze badges














  • $begingroup$
    Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
    $endgroup$
    – MikeY
    Mar 27 at 23:34
















  • $begingroup$
    Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
    $endgroup$
    – MikeY
    Mar 27 at 23:34















$begingroup$
Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
$endgroup$
– MikeY
Mar 27 at 23:34




$begingroup$
Thanks for the benchmark. I started down the Ordering road, but went for parsimony of expression. Mild bummer that it is at least twice as slow as the best method.
$endgroup$
– MikeY
Mar 27 at 23:34

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript