Luis Cordn Tortulec
Luis Cordn Tortulec

Reputation: 93

How to use setSortOrderProvider in Grid Vaadin 8?

Im trying to use Grid Component. I need to define the order of a column, I'm using this proyect: https://github.com/vaadin/tutorial/tree/v8-step4

And I add this code:

Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
    .setCaption("Name")
    .setSortOrderProvider(direction -> Stream.of(
            new QuerySortOrder("lastName", direction)
            ));

grid.setSortOrder(GridSortOrder.asc(name));

But I'm not getting the expected results, I'm getting ordered by firstName and then by lastName but i need the results ordered by lastName.

Have you had the same problem? How have you solved it?

Thank you.

Upvotes: 6

Views: 2028

Answers (2)

Luis Cordn Tortulec
Luis Cordn Tortulec

Reputation: 93

I use this implementation of setComparator and it's working. : )

Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
        .setCaption("Name")
        .setComparator(new SerializableComparator<Customer>() {

            @Override
            public int compare(Customer arg0, Customer arg1) {
                return arg0.getLastName().compareTo(arg1.getLastName());
            }
        });

With Lambda:

.setComparator((customer0, customer1) -> {
            return customer0.getLastName().compareTo(customer1.getLastName());
        });

and this other option:

Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
        .setCaption("Name")
        .setComparator(grid.getColumn("lastName").getComparator(SortDirection.ASCENDING));

Upvotes: 3

Steffen Harbich
Steffen Harbich

Reputation: 2749

I digged into the code and found out that you need you need to call setComparator instead of the setSortOrderProvider. The former is intended for in-memory data providers. Unfortunately, it's a little bit confusing and not really well documented.

Upvotes: 5

Related Questions