Subscribe Bookmark RSS Feed

How to Sort Open Tables by Table Name

csoon1

Community Trekker

Joined:

Feb 9, 2016

I am opening multiple tables at once. Once opened, I loop through all open tables from 1 to N Table() and store the references to each table on a list. However as I loop through each open table, I am not sure how it is arranged in the memory. Bottom line, I want the entries on my list to be sorted according to the table name. Is there a way for me to sort the open tables before storing them to a list? Or to put it in a different angle, what's the best way to sort the table references inside my list (if assuming they are stored on my list at a random order)?

9 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

Here's one way:

 

openTables = {};
for(t=1, t<=NTable(), t++, InsertInto(openTables, DataTable(t) << getName));
dt = NewTable("Invisible", NewColumn("Table Name", Character, Values(openTables)), Invisible);
dt << Sort(By("Table Name"), Ascending, ReplaceTable);
openTables = Column(dt, "Table Name") << getValues;
Close(dt, NoSave);

 

ram_asra_gmail_

Community Trekker

Joined:

Mar 21, 2013

How can i sort the table by  values in a list instead of ascending or descending?

lets assume i have unique column values  in a list. and the order of values in the list is niether ascending nor descending. how i can use this list to sort the table?

Thanks in advance.

Ram

ian_jmp

Staff

Joined:

Jun 23, 2011

I'm not sure I understand what you are asking, unfortunately.

Suppose I have lst of character string values, myList = {"alpha", "gamma", "beta"} and a table, "My Table", with a column called "Vals" that has one of these three values in each cell.

Do you want to sort 'My Table" so all the rows with "alpha" come first, all the rows with "beta" come last, and all the rows with "gamma" come in between?

pmroz

Super User

Joined:

Jun 23, 2011

You would use the value ordering property of a column to do this.  Here's an example:

 

sort_order_list = {"XXX", "AAA", "DDD", "GGG", "YYY", "QQQ"};

dt = New Table( "Untitled 15", Add Rows( 6 ), 
	New Column( "To Be Sorted", Character, "Nominal", 
		Set Values( {"AAA", "DDD", "GGG", "QQQ", "XXX", "YYY"} )
	)
);

column(dt, "To Be Sorted") << set property("Value Ordering", sort_order_list);

Now when you sort the column To Be Sorted it will be sorted by the order of the items in the list.

csoon1

Community Trekker

Joined:

Feb 9, 2016

@Ian, after the table names are sorted and stored on the openTables list, how can I reference the table? For example, is it possible to do this:

For(a=1, a <= N Table(), a++,

    tmpDT = Data Table( openTables );

    // ....

);

Separately, would this do the same thing to sort the contents of the list?

Sort List Into( openTables );

regards,

Chris

csoon1 wrote:

I am opening multiple tables at once. Once opened, I loop through all open tables from 1 to N Table() and store the references to each table on a list. However as I loop through each open table, I am not sure how it is arranged in the memory. Bottom line, I want the entries on my list to be sorted according to the table name. Is there a way for me to sort the open tables before storing them to a list? Or to put it in a different angle, what's the best way to sort the table references inside my list (if assuming they are stored on my list at a random order)?

ian_jmp

Staff

Joined:

Jun 23, 2011

Sorry, I overlooked the rather nifty 'Sort List Into()''. Clearly I need to reacquaint myself with 'Help > Scripting Index'.

 

So, yes, you can do:

 

 

// Build the sorted list
openTables = {};
for(t=1, t<=NTable(), t++, InsertInto(openTables, DataTable(t) << getName));
SortListInto(openTables);
// Manipulate (Close) each table in this list
for(t=1, t<=NItems(openTables), t++, Close(DataTable(openTables[t]), NoSave); Wait(1));

 

ms

Super User

Joined:

Jun 23, 2011

Here's an alternative if a list of Data Table references, rather than strings, is needed:

 

 

openTables = {};
For(i = 1, i <= N Table(), i++,
    Insert Into(openTables, Data Table(i))
);
Sort List Into(openTables);

// Refer directly to table, e.g.
openTables[1] << Bring Window to Front;

 

csoon1

Community Trekker

Joined:

Feb 9, 2016

Thanks. I should have thought of this before. I had tried sorting the list before but containing only the table names.

Byron_JMP

Staff

Joined:

Apr 26, 2012

Don't sort the tables after they are open!  

Sort the table names before you open them. You can get the table names as a list and then work with them there. Once the list is sorted, open them sequentially and process them.

 

In this script:  https://community.jmp.com/t5/JMP-Scripts/Combine-multiple-txt-or-csv-files/ta-p/24083

I get a list of all of one file type in a directory, and then sequentially open the files.  This works great when the individual files are big, or when there are thousands of files.