question

ernesto daniel AG avatar image
0 Likes"
ernesto daniel AG asked ernesto daniel AG commented

how to sort the rows based on a column that determines the order of them?

Hello I have a problem with sorting the rows of a source table

I would like that based on a column of a global table, the column "ORDEN" determine the order of the complete rows of the source

I know it could be with code but I do not have a clear idea of how to do it

I share a model and images

I hope you can help me

thank you

production-plan.fsm

FlexSim 18.2.3
FlexSim 18.1.2
code
orden.png (1.7 KiB)
orden1.png (19.0 KiB)
orden2.png (9.3 KiB)
production-plan.fsm (37.0 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image
0 Likes"
Joerg Vogel answered ernesto daniel AG commented

Please consider use an SQL approach. A table query can sort a table in the system memory in a local table. Then you clone the result to the original global table.

SQL Queries in References of Manual, please take a look in the section “Getting Data Out of the Query”. The clause of WHERE isn’t necessary at all. You can omit it of the query.

Please ask here again if you need more help on SQL in a new question!

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Benjamin W2 avatar image Benjamin W2 commented ·

Hi @ernesto daniel AG,

Here is an example model that uses @Jörg Vogel's approach. I created a new table called "OrderedPlan" and placed the following code in it's On Reset trigger.

Table current = param(1); //Table node
Table QueryTable = Table.query("Select * FROM Plan ORDER BY Orden ASC");
QueryTable.cloneTo(current);

This gave me an ordered table.

Plan Table:

OrderdPlan Table:

0 Likes 0 ·
onresetcommand.png (24.7 KiB)
plantable.png (12.1 KiB)
orderdplantable.png (10.7 KiB)
ernesto daniel AG avatar image ernesto daniel AG commented ·

hi @Benjamin W2 and @Jörg Vogel

Thank you very much to both of you, I already managed to do what I was looking for

the model works perfectly

0 Likes 0 ·
Aaron C avatar image
0 Likes"
Aaron C answered ernesto daniel AG commented

Assuming that you don't want to sort in Excel and directly import, you have a few options. One that comes to mind is first create a new Global Table. Copy your Source1 table into it. Add an additional column ORDEN and fill in its values to the corresponding ItemName. Then open the OnRunStart script and type the following:

Table newGlobalTable = Table("NewGlobalTableName"); /* This is whatever you name the new table*/
newGlobalTable.sort([5],[1]); /* This will sort the table based on the 5th column in descending order*/
Table sourceTable = getvarnode(Model.find("Source1"), "sequence");

int i = 1;
while (i <= 11) /* This will fill the source table with the new global table values */
{
sourceTable.as(Table)[i][1] = newGlobalTable[i][1];
sourceTable.as(Table)[i][2] = newGlobalTable[i][2]
sourceTable.as(Table)[i][3] = newGlobalTable[i][3];
sourceTable.as(Table)[i][4] = newGlobalTable[i][4];
sourceTable.as(Table)[i][5] = newGlobalTable[i][5];

i++;
}

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

ernesto daniel AG avatar image ernesto daniel AG commented ·

hi @Aaron C

I already saw your answer,

I can not understand what I have to do I can not do what your code shows, could you support me by uploading a code where it does?

please I would appreciate it very much

0 Likes 0 ·
ernesto daniel AG avatar image ernesto daniel AG commented ·

hi @Aaron C

I already made a model with the idea that you recommended me, but it does not order the rows in the way that I specify in the column,

I hope you can review it and give me feedback

I share the model that I made

thank you so much

production-plan1.fsm

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.