cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Neo
Neo
Level VI

How to reorder columns in one table based on row entries in another table?

I have two tables - a subset of columns in table 1 have names which are also rows entries (in a column, say column 7) in table 2.

table 2 rows are sorted in a desired (ascending) order. 

I would like to reorder only those columns in table 1 which have column names occurring in table 2 in the order they are sorted (in column 7) in table 2.

How to achieve this via JSL?

When it's too good to be true, it's neither
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to reorder columns in one table based on row entries in another table?

Here is an example of one  way to handle the question

names default to here(1);
table1 = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

table2 = New Table("Table 2", 
	New Column("Sort Order", character,
		Values({"height","weight","sex"})
	)
);

For( i = N Rows( table2 ), i >= 1, i--,
	Try(
		table1 << go to( Column( table2, 1 )[i] );
		table1 << Move Selected Columns( To first );
	)
);
Jim

View solution in original post

4 REPLIES 4
jthi
Super User

Re: How to reorder columns in one table based on row entries in another table?

Few questions:

  1. Where should the columns be sorted?
    1. Should they be sorted in place?
    2. Should they be first columns in the table?
    3. Should they be last columns in the table?
    4. Should they start from the first column to be sorted in the table?
  2. Is the order in table 2 based on column 7 or some other columns?
  3. Are there example data tables (table1 and table 2) and then table 1 with desired results?
-Jarmo
Neo
Neo
Level VI

Re: How to reorder columns in one table based on row entries in another table?

@jthi All valid questions. I will try to answer them below.

 

  1. Where should the columns be sorted?  Do you mean re-order? Re-ordered columns should be in table 1 (not new table)?
    1. Should they be sorted in place? Table 1 has more columns than those which need reordering. See next.
    2. Should they be first columns in the table? Can do.
    3. Should they be last columns in the table? Can do. I can handle both option 2 and 3.
    4. Should they start from the first column to be sorted in the table? The order of (re-ordered) columns in table 1 should be in the order they appear as row entries in (col 7 of) table 2.
  2. Is the order in table 2 based on column 7 or some other columns? I smell confusion here. I will try again. Row entries in column 7 of table 2 is ordered in a desired order. Row entries in column 7 of table 2  are also column names in table 1. I want to re-order the these "common" columns in table 1 in the sequence they appear in table 2.
  3. Are there example data tables (table1 and table 2) and then table 1 with desired results? I cannot share actual data, but I will try to cook something up and share. 
When it's too good to be true, it's neither
Neo
Neo
Level VI

Re: How to reorder columns in one table based on row entries in another table?

@jthi Thanks

When it's too good to be true, it's neither
txnelson
Super User

Re: How to reorder columns in one table based on row entries in another table?

Here is an example of one  way to handle the question

names default to here(1);
table1 = 
// Open Data Table: semiconductor capability.jmp
// → Data Table( "semiconductor capability" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

table2 = New Table("Table 2", 
	New Column("Sort Order", character,
		Values({"height","weight","sex"})
	)
);

For( i = N Rows( table2 ), i >= 1, i--,
	Try(
		table1 << go to( Column( table2, 1 )[i] );
		table1 << Move Selected Columns( To first );
	)
);
Jim