Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.4K Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

Sort a table with respect to other?

Posted 8 years ago

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:
POSTED BY: S G
3 Replies

I would do the following:

B[[Permute[Ordering[B[[All,{2,3}]]], Ordering[A[[All,{2,3}]]]]]]
POSTED BY: Carl Woll

You can try this way:

Bnew = With[{restA = Map[Rest, A]},
  Sort[B, OrderedQ[{Position[restA, Rest[#1]],
      Position[restA, Rest[#2]]}] &]]
POSTED BY: Gianluca Gorni
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard