Message Boards Message Boards

0
|
7464 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How can I order a list of rules by given list?

Posted 9 years ago

HI I have two list. list 1 is a list of symbols, and list 2 uses those symbols as patterns in an arbitrary order. How can reorder list 2 by list 1? I'd like to specifically use list 1 as the basis off the transformation, as the actual usage case grows in complexity.

list1={a,b,c}

list2={b->3,a->2,c->4}
output should be. {a->2,b->3,c->4)

Thanks

POSTED BY: Pat Mac
2 Replies
Posted 9 years ago

Great,

Thank you, I was trying to do this with Cases[] but just ended up further confused. Your technique is perfect.

POSTED BY: Pat Mac

Sort[list2,OrderedQ[{Position[list1, #1[[1]]], Position[list1, #2[[1]]]}] &]

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract