Vaadin Grid comparator conflict with server-side pagination

I’m using a Vaadin Grid with server-side pagination and adding a custom comparator so that the value "ALL" always appears first when sorting ascending, and last when sorting descending.

.setComparator((j1, j2) → {
String c1 = j1.getCustomer();
String c2 = j2.getCustomer();
if (“ALL”.equalsIgnoreCase(c1))
return -1;
if (“ALL”.equalsIgnoreCase(c2))
return 1;
if (c1 == null && c2 == null) return 0;
if (c1 == null) return 1;
if (c2 == null) return -1;
return c1.compareToIgnoreCase(c2);
})

There seems to be a conflict between the server-side sorting and the UI comparator.
For example, the backend fetch might return the first page with numeric customers first, followed by those starting with “A” — but I should actually be getting the "ALL" rows first.

Has anyone run into this kind of Vaadin server/UI sorting mismatch before? How did you handle custom ordering like this when using server-side pagination?
I just want “ALL” to show up first for ascending order, and “ALL” should show last at descending order

Just a guess, related to [Grid] Column ignores custom reversed comparator implementation · Issue #3926 · vaadin/flow-components · GitHub

Actually, it’s not working for ascending order either.
The issue started to appear after I added more customers with numeric names. Please take a look at the updated description to see what I mean.

You mean lazy loading? If yes, the backend / your SQL should do the lifting… there is normally no comparator of Vaadin involved anymore (except in your hack from yesterday :sweat_smile:)

I believe I’ve fixed the issue.
I removed the setComparator from the column and modified my query instead.
I added a new method to check if the user is sorting by the Customer column.
If so, it uses a new query with an ORDER BY clause that ensures "ALL" always appears first.

1 Like