How to generate a column that depends on the previous row value?

In my application, I’ve implemented filter and sort listeners so that when they are triggered, I calculate the values manually for the columns and the column just uses that. The issue is that the sorting or filtering changes the calculation and I’m having trouble obtaining the order in which the rows are shown in the grid. I also need it to work in a TreeGrid.
What is the best way to do this? Either getting the sorted rows or generating the the value.
Thanks.

@brunoagretti can you share the code snippet for your grid and these listeners?

Is the grid configured with an in-memory data provider or does it lazy-load items from the database?

Continuing on what @Leif started. If you use in-memory data, this should be trivial as GridListDataView has methods getNextItem(item) and getPreviousItem(item);

/**
 * Gets the item before given item from the filtered and sorted data.
 * <p>
 * Note! Item might be present in the data set, but be filtered out or be
 * the first item so that the previous item won't be available.
 *
 * @param item
 *            item to get previous for
 * @return previous item if available, else empty optional if item doesn't
 *         exist or not in current filtered items
 *
 * @see #getNextItem(Object)
 */

Thank you for your responses. Here’s a simplified version of my code:

public class MyCustomTreeGrid extends TreeGrid<MyRow> {

    public MyCustomTreeGrid(){
        super();

        addSortListener(sortEvent -> {
            List<MyRow> currentGridItems = new ArrayList<>(this.getTreeData().getRootItems());
            
            recalculateColumnsThatDependOnRowOrder(currentGridItems);
            
            this.getDataProvider().refreshAll();
        });
    }

    private void recalculateColumnsThatDependOnRowOrder(List<MyRow> orderedItems) {
        // Implementation to recalculate the column values based on the ordered items
    }
}

The issue I’m facing is specifically with TreeGrid, which doesn’t support GridListDataView. I need to obtain the items in the order they are shown in the TreeGrid so that I can pass this ordered collection of MyRow to my recalculateColumnsThatDependOnRowOrder method. This method recalculates the values for a column that depends on the order of rows.

Do you have any suggestions on how to handle this in TreeGrid? Thanks!

Additionally, I want to clarify that MyCustomTreeGrid is using an in-memory data provider, and that MyRow has a field List<MyRow> subrows