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!
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?
@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:
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.
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.
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!
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!
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.