Child pages
  • The Servoy Foundset

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note

See also the loadRecords API in the reference guide for complete usage options.

Sorting

 

All foundsets contain a sorting definition that determines the order in which records are loaded and displayed. Sorting is always expressed in the ORDER BY clause of a foundset's query and thus handled by the database. 

A foundset's sorting definition is encapsulated in a String property, which can be programmatically read using the getCurrentSort method, and written using the sort method.

 

Parameters for this property include an ordered list of one or more data providers, each of which having a sort direction, either ascending or descending. The string takes a form such that each data provider and its sort direction are separated by white space. The direction is abbreviated either asc or desc. Multiple data providers are separated by commas.

...

Code Block
'column1 asc, column2 desc'	// Sort on column1 ascending, then column2 desceding

 

The order of the data providers determines their relative priority when sorting, such that when two records contain the same value for a higher priority data provider, the sorting will be deferred the next lowest priority data provider.

 

Example: The following sort string will sort, first on last name, and second on first name.

Code Block
foundset.sort('last_name asc, first_name asc');

...

The result is that all records are sorted by last name. But in the case where the last names are the same, then the first name is used.
|| last_name || first_name || 

Sloan

Zachary

Smith

Jane

Smith

Jon

Snead

Aaron

Available Data Provider Types

The following data provider types may be used as sort criteria:

...

Example: Sort a customers foundset based on the number of orders each customer has, in this case a related aggregation.

Code Block
foundset.sort('customers_to_orders.order_count asc');
SELECT customers.customerid FROM customers 
INNER JOIN orders ON customers.customerid=orders.customerid 
GROUP BY customers.customerid ORDER BY count(orders.orderid) ASC

Results in the following query:

Code Block
SELECT customers.customerid FROM customers 
INNER JOIN orders ON customers.customerid=orders.customerid 
GROUP BY customers.customerid ORDER BY count(orders.orderid) ASC
Tip

Sorting on related columns and aggregates changes is simple and powerful. However this changes the nature of the foundset's query. One should be advised of this and ensure that the database is tuned accordingly.

Scrolling Result Set