Sorter Node | How To Sort Data While "Criterion Columns Numbers/Count Are Changing - KNIME (5.8.2 LTS)

Hello All, Hope all of you are doing well, I stuck in a black hole.

I cooked a dataset which having changing column count as per logic/selection. I want to sort this dataset based on some columns but these columns number is changing as shown below.

Scenario1 Scenario2 Scenario3 Scenario4 Scenario5
Column1 Column1 Column1 Column1 Column1
Column2 Sno Column2 Column2 Column2
Column3 Column3 Sno Column3
Column4 Column4 Column4
Column5 Sno Column5
Column6 Sno
Sno

1 work around for this is to concatenate all columns in each scenario and create one more new column and then sort data on this new column but I am not able to implement this work around.

I tried few more things but I am not able to solve it.

Kindly help me to solve it or suggest a work around.

Kindly help me

I assume the columns in each scenario are actual columns with data. If so, why doesn’t the Sorter node work for what you want? What kind of data do you have?

My apologies for replying late

No, Only 2 Columns (Column1 and Sno) are actual, rest I am adding / removing as per scenario. Let me explain further

Scenario2 is the ideal situation

On the selection of certain value in Column1, 2nd scenario (above mention scenario4) arise and Column2 added

On the selection of certain value in Column2, 2nd scenario (not in above mention scenarios) arise and Column3 Added

On the selection of certain value in Column3, 3rd scenario (above mention scenario3 ) arise and Column4 Added

On the selection of certain value in Column4, 4th scenario (above mention scenario5 ) arise and Column4 Added

On the selection of certain value in Column5, 5th scenario (above mention scenario6 ) arise and Column5 Added

And it is all happing in one workflow/cmponent

While reply a new idea arise in my mind, let me test it.

So the task is to sort the table that the column Sno is always the last one?

Yes, you are right, Sno always will be the last column.

are your columns hierarchically sorted?
Do you always sort by column 1 first, then by column 2, then by column 3?
and, do you know how many columns you will have at most for sorting?

easiest solution is as follows:

use the Column Name Extractor node, second output. concat this table with a table creator node that contains blank columns equal to the maximum number of columns you expect, matching the naming patter of the columns generated by the Column Name Extractor node (second port).
this way, you will ensure that whatever amount of columns you have, the maximum will be present at all times.
then you can set up a sorter that simply sorts by ALL columns from left to right.
after that, you use a reference column filter node to get rid of all added columns again.
and in the last step, you use the column name replacer node to exchange the generic names back to your original names (you might need to transpose the top output of the Column Name Extractor for that (dont recall)).


if you are not talking about sorting the data but sorting the columns: just split of the sno column and use a Column Appender to add it back to the end.
Or the better approach if you know all column names: Reference Column Re-(Sorter)

3 Likes

Thanks for reply

You are right, Column1 will always be the first then Column2, then Column3 ….. and so on.

Yes, I am talking about the Data Sorting only, let me test your suggested solution.

instead of the table creater + concat trick, the more modern approach I guess is the Table Validator node to ensure the columns are created

2 Likes

Event though the solution is already available, here’s my approach

4 Likes

hi, @ActionAndi Thanks for another idea.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.