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)?

6 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

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.