# Sort a table with respect to other?

Posted 1 year ago
1139 Views
|
3 Replies
|
1 Total Likes
|
 Hi, I have two 36 X 3 tables. First columns in both of them are independent terms/numbers, while second and third columns have same indices (numbers) from 1 - 6, but in different orders. I want to arrange the rows of table B in the same order as in A, with respect to second and third elements of them. For example if A = {{X, 2, 1}, {Y, 2, 3}, {Z, 2, 4}...} and B = {{x, 2, 3}, {y, 2, 4}, {z, 2, 1}....} I want B to be rearranged to B_new = {{z, 2, 1}, {x, 2, 3}, {y, 2, 4}....} Will be thankful for any suggestion. Attached is the *.nb file having these tables. Attachments:
3 Replies
Sort By:
Posted 1 year ago
 Step 1. for every element in A, there is a corresponding element in B. The first step is to write a function that takes in an element from A and gives the corresponding element from B. To do this, first write a function that checks whether an element from A and an element from B match. Then use the Select function to make the function in Step 1. Step 2. Go thru every element in A and replace it with the corresponding element in B. The cleanest way to do this is with Map. You may prefer using a For loop or using Replace depending on your coding style. I'm not sure what your programming experience is. Can you write a function that takes two elements (one from A and one from B) and checks whether they match each other?
 You can try this way: Bnew = With[{restA = Map[Rest, A]}, Sort[B, OrderedQ[{Position[restA, Rest[#1]], Position[restA, Rest[#2]]}] &]] 
 I would do the following: B[[Permute[Ordering[B[[All,{2,3}]]], Ordering[A[[All,{2,3}]]]]]]