Sorting a list while keeping its correspondence to another list
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
add a comment |
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
values[[Ordering@ids]]
andSortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 '18 at 17:53
add a comment |
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
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
list-manipulation
edited Dec 2 '18 at 16:49
asked Dec 2 '18 at 16:40
user120911
58418
58418
values[[Ordering@ids]]
andSortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 '18 at 17:53
add a comment |
values[[Ordering@ids]]
andSortBy[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
add a comment |
4 Answers
4
active
oldest
votes
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}
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 withWith
. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07
add a comment |
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}}
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 '18 at 18:17
add a comment |
AssociationThread[ids, values] // KeySort
Edit
To extract keys and values
sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]
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
add a comment |
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}}
1
A problem that arises here withTranspose
is is that it unpacks arrays:ids
is a list of integers andvalues
is a list of machine reals. So both can be packed separately; but notTranspose@{ids, values}
as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16
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%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
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}
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 withWith
. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07
add a comment |
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}
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 withWith
. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07
add a comment |
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}
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}
edited Dec 2 '18 at 17:06
answered Dec 2 '18 at 16:48
Henrik Schumacher
48.8k467139
48.8k467139
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 withWith
. Please try again.
– Henrik Schumacher
Dec 2 '18 at 17:07
add a comment |
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 withWith
. 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
add a comment |
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}}
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 '18 at 18:17
add a comment |
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}}
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 '18 at 18:17
add a comment |
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}}
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}}
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
add a comment |
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
add a comment |
AssociationThread[ids, values] // KeySort
Edit
To extract keys and values
sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]
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
add a comment |
AssociationThread[ids, values] // KeySort
Edit
To extract keys and values
sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]
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
add a comment |
AssociationThread[ids, values] // KeySort
Edit
To extract keys and values
sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]
AssociationThread[ids, values] // KeySort
Edit
To extract keys and values
sorted = AssociationThread[ids, values] // KeySort
sortedIds = Keys[sorted]
sortedValues = Values[sorted]
edited Dec 2 '18 at 17:24
answered Dec 2 '18 at 16:46
Rohit Namjoshi
7871111
7871111
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
add a comment |
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
add a comment |
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}}
1
A problem that arises here withTranspose
is is that it unpacks arrays:ids
is a list of integers andvalues
is a list of machine reals. So both can be packed separately; but notTranspose@{ids, values}
as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16
add a comment |
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}}
1
A problem that arises here withTranspose
is is that it unpacks arrays:ids
is a list of integers andvalues
is a list of machine reals. So both can be packed separately; but notTranspose@{ids, values}
as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16
add a comment |
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}}
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}}
answered Dec 2 '18 at 18:07
Okkes Dulgerci
3,9851816
3,9851816
1
A problem that arises here withTranspose
is is that it unpacks arrays:ids
is a list of integers andvalues
is a list of machine reals. So both can be packed separately; but notTranspose@{ids, values}
as it has mixed data types. This may become important when processing large datasets.
– Henrik Schumacher
Dec 2 '18 at 18:16
add a comment |
1
A problem that arises here withTranspose
is is that it unpacks arrays:ids
is a list of integers andvalues
is a list of machine reals. So both can be packed separately; but notTranspose@{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
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.
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.
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%2f187161%2fsorting-a-list-while-keeping-its-correspondence-to-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
values[[Ordering@ids]]
andSortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 '18 at 17:53