Sorting a list while keeping its correspondence to another list
up vote
5
down vote
favorite
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 |
up vote
5
down vote
favorite
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 at 17:53
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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 at 16:49
asked Dec 2 at 16:40
user120911
53818
53818
values[[Ordering@ids]]
andSortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 at 17:53
add a comment |
values[[Ordering@ids]]
andSortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 at 17:53
values[[Ordering@ids]]
and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 at 17:53
values[[Ordering@ids]]
and SortBy[Transpose[{ids, values}], First] == Transpose[{Sort@ids, values[[Ordering@ids]]}]
– user1066
Dec 2 at 17:53
add a comment |
4 Answers
4
active
oldest
votes
up vote
5
down vote
accepted
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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything withWith
. Please try again.
– Henrik Schumacher
Dec 2 at 17:07
add a comment |
up vote
4
down vote
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 at 18:17
add a comment |
up vote
4
down vote
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 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
add a comment |
up vote
2
down vote
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 at 18:16
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything withWith
. Please try again.
– Henrik Schumacher
Dec 2 at 17:07
add a comment |
up vote
5
down vote
accepted
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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything withWith
. Please try again.
– Henrik Schumacher
Dec 2 at 17:07
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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 at 17:06
answered Dec 2 at 16:48
Henrik Schumacher
46.8k466134
46.8k466134
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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything withWith
. Please try again.
– Henrik Schumacher
Dec 2 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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything withWith
. Please try again.
– Henrik Schumacher
Dec 2 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 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 at 16:59
Sorry, I missed a semicolon that was necessary after wrapping everything with
With
. Please try again.– Henrik Schumacher
Dec 2 at 17:07
Sorry, I missed a semicolon that was necessary after wrapping everything with
With
. Please try again.– Henrik Schumacher
Dec 2 at 17:07
add a comment |
up vote
4
down vote
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 at 18:17
add a comment |
up vote
4
down vote
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 at 18:17
add a comment |
up vote
4
down vote
up vote
4
down vote
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 at 16:45
Johu
3,6531037
3,6531037
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 at 18:17
add a comment |
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 at 18:17
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 at 18:17
See also my remark under Okke's post.
– Henrik Schumacher
Dec 2 at 18:17
add a comment |
up vote
4
down vote
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 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
add a comment |
up vote
4
down vote
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 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
add a comment |
up vote
4
down vote
up vote
4
down vote
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 at 17:24
answered Dec 2 at 16:46
Rohit Namjoshi
7031110
7031110
This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
add a comment |
This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 at 17:00
This looks simple, but how do I work with these afterwards?
– user120911
Dec 2 at 17:00
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
@user120911 Added sorted key / value extraction.
– Rohit Namjoshi
Dec 2 at 17:24
add a comment |
up vote
2
down vote
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 at 18:16
add a comment |
up vote
2
down vote
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 at 18:16
add a comment |
up vote
2
down vote
up vote
2
down vote
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 at 18:07
Okkes Dulgerci
3,7131716
3,7131716
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 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 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 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 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 at 17:53