The answer is no and it wouldn't make much sense else. The grid.setItems
method internally creates a ListDataProvider
which is a an in-memory data provider. Because it holds all the data in a list, it can easily sort the data with a comparator (coming from the grid column definition or supplied to the list data provider). So, Vaadin can only help you with sorting when it is OK for you to hold all the data in memory. If you have huge amounts of rows (e.g. a million) this is inefficient because you have a lot memory and CPU consumption. This is where your custom data provider comes in.
The idea of DataProvider
is to delegate the data retrieval to a backend system like a database. For example, SQL databases allow to specify an "ORDER BY" clause in the SQL query which you could use to sort data via the backend. The data base itself is very efficient regarding resource consumption, assuming you have the right indexes applied.
So what is your actual data source? Maybe you can share your data provider code you have done so far.
EDIT
Excerpt from ListDataProvider
:
@Override
public Stream<T> fetch(Query<T, SerializablePredicate<T>> query) {
Stream<T> stream = getFilteredStream(query);
Optional<Comparator<T>> comparing = Stream
.of(query.getInMemorySorting(), sortOrder)
.filter(c -> c != null)
.reduce((c1, c2) -> c1.thenComparing(c2));
if (comparing.isPresent()) {
stream = stream.sorted(comparing.get());
}
return stream.skip(query.getOffset()).limit(query.getLimit());
}
@Override
public int size(Query<T, SerializablePredicate<T>> query) {
return (int) getFilteredStream(query).count();
}
To answer your questions from the comment: Yes, ListDataProvider
does the sort as you can see in its source code. And with your SQL database: Yes, it is the preferred way.