Sorting a list while keeping its correspondence to another list












5














I have the following two lists:



ids = {5, 11, 17, 24, 31, 37, 40, 39, 38, 33, 32, 25}

values = {0.0351563, 0.131836, 0.086792, 0.0637894, 0.065752, 0.191388, 0.063796, 0.173784, 0.0503769, 0.0875244, 0.0146484, 0.0351563}


where entries in values correspond to entries in ids.



I now wish to sort ids in ascending order while maintaining the correspondence to values, such that elements in values must be changed according to the changes made to ids.



How can that be achieved?










share|improve this question
























  • values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
    – user1066
    Dec 2 '18 at 17:53
















5














I have the following two lists:



ids = {5, 11, 17, 24, 31, 37, 40, 39, 38, 33, 32, 25}

values = {0.0351563, 0.131836, 0.086792, 0.0637894, 0.065752, 0.191388, 0.063796, 0.173784, 0.0503769, 0.0875244, 0.0146484, 0.0351563}


where entries in values correspond to entries in ids.



I now wish to sort ids in ascending order while maintaining the correspondence to values, such that elements in values must be changed according to the changes made to ids.



How can that be achieved?










share|improve this question
























  • values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
    – user1066
    Dec 2 '18 at 17:53














5












5








5


1





I have the following two lists:



ids = {5, 11, 17, 24, 31, 37, 40, 39, 38, 33, 32, 25}

values = {0.0351563, 0.131836, 0.086792, 0.0637894, 0.065752, 0.191388, 0.063796, 0.173784, 0.0503769, 0.0875244, 0.0146484, 0.0351563}


where entries in values correspond to entries in ids.



I now wish to sort ids in ascending order while maintaining the correspondence to values, such that elements in values must be changed according to the changes made to ids.



How can that be achieved?










share|improve this question















I have the following two lists:



ids = {5, 11, 17, 24, 31, 37, 40, 39, 38, 33, 32, 25}

values = {0.0351563, 0.131836, 0.086792, 0.0637894, 0.065752, 0.191388, 0.063796, 0.173784, 0.0503769, 0.0875244, 0.0146484, 0.0351563}


where entries in values correspond to entries in ids.



I now wish to sort ids in ascending order while maintaining the correspondence to values, such that elements in values must be changed according to the changes made to ids.



How can that be achieved?







list-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 2 '18 at 16:49

























asked Dec 2 '18 at 16:40









user120911

58418




58418












  • values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
    – user1066
    Dec 2 '18 at 17:53


















  • values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
    – user1066
    Dec 2 '18 at 17:53
















values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 '18 at 17:53




values[[Ordering@ids]] and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 '18 at 17:53










4 Answers
4






active

oldest

votes


















5














With[{p = Ordering[ids]},
idsnew = ids[[p]];
valuesnew = values[[p]];
]
idsnew
valuesnew



{5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}



{0.0351563, 0.131836, 0.086792, 0.0637894, 0.0351563, 0.065752, 0.0146484, 0.0875244, 0.191388, 0.0503769, 0.173784, 0.063796}







share|improve this answer























  • This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
    – user120911
    Dec 2 '18 at 16:59










  • Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
    – Henrik Schumacher
    Dec 2 '18 at 17:07



















4














I would keep them together while sorting.



SortBy[Transpose[{ids, values}], First]



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}






share|improve this answer





















  • See also my remark under Okke's post.
    – Henrik Schumacher
    Dec 2 '18 at 18:17



















4














AssociationThread[ids, values] // KeySort


Edit
To extract keys and values



sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]





share|improve this answer























  • This looks simple, but how do I work with these afterwards?
    – user120911
    Dec 2 '18 at 17:00










  • @user120911 Added sorted key / value extraction.
    – Rohit Namjoshi
    Dec 2 '18 at 17:24



















2














Since Sort sort it by the first entry by default



Sort@Transpose@{ids, values}



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}







share|improve this answer

















  • 1




    A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
    – Henrik Schumacher
    Dec 2 '18 at 18:16











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%2f187161%2fsorting-a-list-while-keeping-its-correspondence-to-another-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














With[{p = Ordering[ids]},
idsnew = ids[[p]];
valuesnew = values[[p]];
]
idsnew
valuesnew



{5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}



{0.0351563, 0.131836, 0.086792, 0.0637894, 0.0351563, 0.065752, 0.0146484, 0.0875244, 0.191388, 0.0503769, 0.173784, 0.063796}







share|improve this answer























  • This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
    – user120911
    Dec 2 '18 at 16:59










  • Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
    – Henrik Schumacher
    Dec 2 '18 at 17:07
















5














With[{p = Ordering[ids]},
idsnew = ids[[p]];
valuesnew = values[[p]];
]
idsnew
valuesnew



{5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}



{0.0351563, 0.131836, 0.086792, 0.0637894, 0.0351563, 0.065752, 0.0146484, 0.0875244, 0.191388, 0.0503769, 0.173784, 0.063796}







share|improve this answer























  • This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
    – user120911
    Dec 2 '18 at 16:59










  • Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
    – Henrik Schumacher
    Dec 2 '18 at 17:07














5












5








5






With[{p = Ordering[ids]},
idsnew = ids[[p]];
valuesnew = values[[p]];
]
idsnew
valuesnew



{5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}



{0.0351563, 0.131836, 0.086792, 0.0637894, 0.0351563, 0.065752, 0.0146484, 0.0875244, 0.191388, 0.0503769, 0.173784, 0.063796}







share|improve this answer














With[{p = Ordering[ids]},
idsnew = ids[[p]];
valuesnew = values[[p]];
]
idsnew
valuesnew



{5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}



{0.0351563, 0.131836, 0.086792, 0.0637894, 0.0351563, 0.065752, 0.0146484, 0.0875244, 0.191388, 0.0503769, 0.173784, 0.063796}








share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 2 '18 at 17:06

























answered Dec 2 '18 at 16:48









Henrik Schumacher

48.9k467139




48.9k467139












  • This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
    – user120911
    Dec 2 '18 at 16:59










  • Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
    – Henrik Schumacher
    Dec 2 '18 at 17:07


















  • This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
    – user120911
    Dec 2 '18 at 16:59










  • Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
    – Henrik Schumacher
    Dec 2 '18 at 17:07
















This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
– user120911
Dec 2 '18 at 16:59




This generates an error for me: Set::write: Tag Times in valuesnew {5,11,17,24,25,31,32,33,37,38,39,40} is Protected.
– user120911
Dec 2 '18 at 16:59












Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07




Sorry, I missed a semicolon that was necessary after wrapping everything with With. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07











4














I would keep them together while sorting.



SortBy[Transpose[{ids, values}], First]



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}






share|improve this answer





















  • See also my remark under Okke's post.
    – Henrik Schumacher
    Dec 2 '18 at 18:17
















4














I would keep them together while sorting.



SortBy[Transpose[{ids, values}], First]



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}






share|improve this answer





















  • See also my remark under Okke's post.
    – Henrik Schumacher
    Dec 2 '18 at 18:17














4












4








4






I would keep them together while sorting.



SortBy[Transpose[{ids, values}], First]



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}






share|improve this answer












I would keep them together while sorting.



SortBy[Transpose[{ids, values}], First]



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 2 '18 at 16:45









Johu

3,6681037




3,6681037












  • See also my remark under Okke's post.
    – Henrik Schumacher
    Dec 2 '18 at 18:17


















  • See also my remark under Okke's post.
    – Henrik Schumacher
    Dec 2 '18 at 18:17
















See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 '18 at 18:17




See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 '18 at 18:17











4














AssociationThread[ids, values] // KeySort


Edit
To extract keys and values



sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]





share|improve this answer























  • This looks simple, but how do I work with these afterwards?
    – user120911
    Dec 2 '18 at 17:00










  • @user120911 Added sorted key / value extraction.
    – Rohit Namjoshi
    Dec 2 '18 at 17:24
















4














AssociationThread[ids, values] // KeySort


Edit
To extract keys and values



sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]





share|improve this answer























  • This looks simple, but how do I work with these afterwards?
    – user120911
    Dec 2 '18 at 17:00










  • @user120911 Added sorted key / value extraction.
    – Rohit Namjoshi
    Dec 2 '18 at 17:24














4












4








4






AssociationThread[ids, values] // KeySort


Edit
To extract keys and values



sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]





share|improve this answer














AssociationThread[ids, values] // KeySort


Edit
To extract keys and values



sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 2 '18 at 17:24

























answered Dec 2 '18 at 16:46









Rohit Namjoshi

7871112




7871112












  • This looks simple, but how do I work with these afterwards?
    – user120911
    Dec 2 '18 at 17:00










  • @user120911 Added sorted key / value extraction.
    – Rohit Namjoshi
    Dec 2 '18 at 17:24


















  • This looks simple, but how do I work with these afterwards?
    – user120911
    Dec 2 '18 at 17:00










  • @user120911 Added sorted key / value extraction.
    – Rohit Namjoshi
    Dec 2 '18 at 17:24
















This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 '18 at 17:00




This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 '18 at 17:00












@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 '18 at 17:24




@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 '18 at 17:24











2














Since Sort sort it by the first entry by default



Sort@Transpose@{ids, values}



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}







share|improve this answer

















  • 1




    A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
    – Henrik Schumacher
    Dec 2 '18 at 18:16
















2














Since Sort sort it by the first entry by default



Sort@Transpose@{ids, values}



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}







share|improve this answer

















  • 1




    A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
    – Henrik Schumacher
    Dec 2 '18 at 18:16














2












2








2






Since Sort sort it by the first entry by default



Sort@Transpose@{ids, values}



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}







share|improve this answer












Since Sort sort it by the first entry by default



Sort@Transpose@{ids, values}



{{5, 0.0351563}, {11, 0.131836}, {17, 0.086792}, {24, 0.0637894}, {25,
0.0351563}, {31, 0.065752}, {32, 0.0146484}, {33, 0.0875244}, {37,
0.191388}, {38, 0.0503769}, {39, 0.173784}, {40, 0.063796}}








share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 2 '18 at 18:07









Okkes Dulgerci

3,9951816




3,9951816








  • 1




    A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
    – Henrik Schumacher
    Dec 2 '18 at 18:16














  • 1




    A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
    – Henrik Schumacher
    Dec 2 '18 at 18:16








1




1




A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16




A problem that arises here with Transpose is is that it unpacks arrays: ids is a list of integers and values is a list of machine reals. So both can be packed separately; but not Transpose@{ids, values} as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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


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%2f187161%2fsorting-a-list-while-keeping-its-correspondence-to-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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents