sort not working with SQLContainer and FreeformStatementDelegate

I’m using a table with a SQLContainer and a FreeformStatementDelegate in order to do lazy loading.
Everything works fine, except that I can’t sort the table by clicking on one of the column headers.

If I click a column header, an arrow is displayed to indicate that the table is now sorted on that column, but the sort never happens.

By running in debug mode, I noticed that the following methods are executed:

table -> changeVariables(Object source, Map<String, Object> variables)
table -> setSortContainerPropertyId(id, false)
table -> this.sort()
table -> sort(new Object[] { sortContainerPropertyId }, new boolean[]
 { sortAscending });
SQLContainer -> sort(Object[] propertyId, boolean[]
    -> this adds a new sorter by calling sorters.add(new OrderBy((String) propertyId[i]
, asc));
SQLContainer -> refresh();
SqlContainer -> refresh(true);
SQLContainer -> fireContentsChange();

and finally:

MyFreeformStatementDelegate -> getQueryStatement(int offset, int limit);

By searching the forums, docs, examples, etc I found several indications that the FreeformStatementDelegate method should handle the sorting, but they all use something like this:

  private String getOrderByString() {
	        StringBuffer orderBuffer = new StringBuffer("");
	        if (orderBys != null && !orderBys.isEmpty()) {
	            orderBuffer.append(" ORDER BY ");
	            OrderBy lastOrderBy = orderBys.get(orderBys.size() - 1);
	            for (OrderBy orderBy : orderBys) {
	                if (orderBy.isAscending()) {
	                    orderBuffer.append(" ASC");
	                } else {
	                    orderBuffer.append(" DESC");
	                if (orderBy != lastOrderBy) {
	                    orderBuffer.append(", ");
	        return orderBuffer.toString();

Therefore I also implemented this method and call the getOrderByString() method in the getQueryStatement() method, but the orderBys array never gets populated, resulting in an empty orderby string.

My getQueryStatement() method:

public StatementHelper getQueryStatement(int offset, int limit) throws UnsupportedOperationException {
		StatementHelper sh = new StatementHelper();
		StringBuffer query = new StringBuffer(formatQuery());

		if (offset != 0 || limit != 0) {
			query.append(" LIMIT ").append(limit);
			query.append(" OFFSET ").append(offset);
		return sh;

So, how is it supposed to work / to be implemented ?


I found a workaround by passing the Table to the FreeformStatementDelegate and then using its getSortContainerPropertyId() and isSortAscending() methods in the getOrderByString() method.

I wonder however whether this is recommended ?
Also I wonder why all the information I can find uses the orderBys array and especially why this is working in those examples and not in my code :unsure: