Reverse sorting rows (JAVA)

Hi Guys,

Good day.
I would like to ask for your advice on how will I reverse sort rows on a column. My rows are produced using DataRow execution but I need it to sort it in reverse for my output. Or can I sort its rows directly from the BufferedDataTable? I was trying to use the BufferedDataTableSorter but I am a bit confused with it’s configurations and it seems that it is only capable for Ascending and Descending order. But if there’s anything that I am wrong about this, kindly enlighten me.
Any suggestions would help!

Thanks in advance.

best regards,
Gambit

2 Likes

Hello @Gambit,

thank you for your message. From what I can tell, I generally would suggest to choose the generic <sorter> node. In there you can sort by ROWKEY or any other column and even by multiple columns.
If that does not help you, I would be happy to get more insight into your use case. Maybe you can describe it a bit further or share a screenshot of your workflow/output tables?

Best regards,
Kevin

3 Likes

Hi @Gambit & @kevin_sturm

@kevin_sturm your answer is perfectly valid but I believe that @Gambit would like to implement the sorting inside his own Java node. If this is the case, I would suggest to have a look at the generic -Row Sorter- source code and how it is implemented. It may provide you with the necessary information to integrate row sorting in your own nodes.

The source code is available at the following link:

Hope this helps.

Best

Ael

3 Likes

Hi @kevin_sturm,

Thank you for your suggestion. However, I am currently using Eclipse for my development. and I am having a hard time sort my rows in reverse.

Thanks!
Gambit

Hi @aworker ,

Thank you for this. You are right, I am developing my node using java. I am checking it right now, but I believe it can only sort it in ascending or descending but not in reverse sorting. It might change the sequence of my rows if I use it. It uses BufferedDataTableSorter for this function. But I thank you for this. I’ll try to look for another way.

Thank you and best regards,
Gambit

Hi @Gambit

If I understand well, you therefore would like to “reverse” the order of your rows ? If this is right, I would just recommend a trick:

  1. Add an extra column with numbers starting from 1 to last value equal to number of rows.
  2. Sort your rows in descending order, using this temporary new index column.
  3. Remove this temporary column which should not any more be useful.

Although a hack, would this trick fulfill your needs of sorting in “reverse” order ?

Hope this helps.

Best regards,

Ael

1 Like

With reverse do you mean reverse the order of rows based on how they are ordered in the input?
I created a small demo workflow with a component that does this: reverse sort – KNIME Community Hub

To do that in java, all you’ll need is a Deque:

    @Override
    protected BufferedDataTable[] execute(BufferedDataTable[] inData, ExecutionContext exec) throws Exception {
        BufferedDataTable in = inData[0];

        Deque<DataRow> deq = new LinkedList<>();
        // push to stack
        for (DataRow r : in) {
            deq.push(r);
        }

        BufferedDataContainer container = exec.createDataContainer(in.getDataTableSpec());

        while (!deq.isEmpty()) {
            // pop from stack
           container.addRowToTable(deq.pop()); 
        }
        container.close();
        return new BufferedDataTable[] {container.getTable()};
    }
   
    ```

Edit: I replaced Stack with Deque, because that is faster.
2 Likes

Hi @aworker ,

Thank you for this. It will help. But if I will try it to revert it back, it will change the positions. I know this is a bit odd, but there is a certain manipulation on data that I want to do after reversing it, then after the manipulation, I want to revert it back.

Thank you and have a nice day.
By the way, I really appreciate your quick response Guys!

Best regards,
Gambit

1 Like

Hi @gab1one ,

Thank you for the immediate response. Yes, reverse the order of rows based on how they are ordered in the input. I will try this on my codes. Thank you for the immediate response! This forum really save many times!

Best regards,
Gambit

1 Like

Hi @Gambit,

Just let you know that @gab1one’s solution should be more efficient if it suits you. The reason for that is that achieving a “reverse” operation is of linear computational complexity (O(N)) whereas a generic ‘sorting’ is most probably of complexity O(N log N) which is higher (Sorting algorithm - Wikipedia).

Go hence for @gab1one’s solution if convenient to your needs.

Best,

Ael

1 Like

Hi @aworker ,

Thanks a lot! I am still new to this, so I really need all the help that I can get. Thanks for the immediate response.

Best regards,
Gambit

1 Like

Hi @gab1one ,

It worked. I now have the desired output.

Hi @aworker ,
Thank you for your references. I am currently reading it.

Thank you guys for the help and immediate response!

Best regards,
Gambit

1 Like

Hello @Gambit,
we are glad that we could help you here and your process works fine now.
Best regards,
Kevin

1 Like

Hi @kevin_sturm ,

Hi! KNIME forum is very helpful. I am glad it exists. I am looking forward for more knowledge and information from you guys! Thank you so much!

Best regards,
Gambit

2 Likes