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













5












$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.
    2 hours ago















5












$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.
    2 hours ago













5












5








5





$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 37 mins ago









MarcoB

37.9k556114




37.9k556114










asked 2 hours ago









M.A.M.A.

645




645











  • $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















$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










2 Answers
2






active

oldest

votes


















5












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




















    4












    $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
      25 mins ago






    • 1




      $begingroup$
      Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
      $endgroup$
      – MikeY
      8 mins ago










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



    );













    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

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5












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

















      5












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















        5












        5








        5





        $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 28 mins ago

























        answered 2 hours ago









        Henrik SchumacherHenrik Schumacher

        58.1k580160




        58.1k580160





















            4












            $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
              25 mins ago






            • 1




              $begingroup$
              Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
              $endgroup$
              – MikeY
              8 mins ago















            4












            $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
              25 mins ago






            • 1




              $begingroup$
              Doh...fixed it. Both ways give the same answer, which leads to sloppy debugging.
              $endgroup$
              – MikeY
              8 mins ago













            4












            4








            4





            $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 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 be Permute[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






            • 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

















            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

            Category:Fedor von Bock Media in category "Fedor von Bock"Navigation menuUpload mediaISNI: 0000 0000 5511 3417VIAF ID: 24712551GND ID: 119294796Library of Congress authority ID: n96068363BnF ID: 12534305fSUDOC authorities ID: 034604189Open Library ID: OL338253ANKCR AUT ID: jn19990000869National Library of Israel ID: 000514068National Thesaurus for Author Names ID: 341574317ReasonatorScholiaStatistics

            Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]Combining two 32-bit integers into one 64-bit integerDetermine if an int is within rangeLossy packing 32 bit integer to 16 bitComputing the square root of a 64-bit integerKeeping integer addition within boundsSafe multiplication of two 64-bit signed integersLeetcode 10: Regular Expression MatchingSigned integer-to-ascii x86_64 assembler macroReverse the digits of an Integer“Add two numbers given in reverse order from a linked list”

            Kiel Indholdsfortegnelse Historie | Transport og færgeforbindelser | Sejlsport og anden sport | Kultur | Kendte personer fra Kiel | Noter | Litteratur | Eksterne henvisninger | Navigationsmenuwww.kiel.de54°19′31″N 10°8′26″Ø / 54.32528°N 10.14056°Ø / 54.32528; 10.14056Oberbürgermeister Dr. Ulf Kämpferwww.statistik-nord.deDen danske Stats StatistikKiels hjemmesiderrrWorldCat312794080n790547494030481-4