New sorting definitions are added to a report with
the
com.crystaldecisions.sdk.occa.report.application.RecordSortController
class.
The order in which the sorting definitions appear
in the
Sorts
collection is important as it represents the order by which sorting definitions
are applied to records. When a sorting definition is removed, the indexes are
re-organized so that the collection is still indexed from zero.
Retrieve the
DataDefController object.
DataDefController dataDefController = rcd.getDataDefController();
Find a field to sort on.
The
IDataDefinition.getResultFields
method returns all fields that have been added to the report design. This
example assumes that the report design contains a
Order_Amount field
named from a table named
Orders.
Fields resultFields = dataDefController.getDataDefinition().getResultFields();
Field sortField = (Field)resultFields.findField("{Orders.Order_Amount}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
Verify that sorting is allowed on the
field.
A sorting definition is
defined by a field and a direction; however, not all fields can be sorted on.
You can use the
canSortOn
method of the
RecordSortController class to
check whether the field can be sorted on.
if (rsc.canSortOn(sortField))
{
...
}
Retrieve the
RecordSortController object.
The
RecordSortController object is
in the
com.crystaldecisions.sdk.occa.report.application.RecordSortController
class.
RecordSortController recordSortController = dataDefController.getRecordSortController();
Create a new
ISort
object, set the field to sort on with the
setSortField
method, and set the direction of the sort.
ISort newSort = new Sort();
newSort.setSortField(sortField);
newSort.setDirection(SortDirection.ascendingOrder);
Call the
add method
of the
RecordSortController class to
add the new sort to the report based on the chosen field.
The first parameter of the
add method
specifies the location of the new sorting definition in the
Sorts
collection. A value of -1 indicates that the new sorting definition will be
added to the end of the
Sorts
collection. The new sorting definition will be applied to records after the
other sorting definitions have been applied.
recordSortController.add(-1, newSort);
Example:
The following code adds a new sorting definition to
a report based on the field
Orders.Order_Amount.
This example checks whether the selected field is a valid field to sort on
before attempting to add the sorting definition.
void addSort(ReportClientDocument rcd) throws ReportSDKException
{
DataDefController dataDefController = rcd.getDataDefController();
Fields resultFields = dataDefController.getDataDefinition().getResultFields();
Field sortField = (Field)resultFields.findField("{Orders.Order_Amount}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
RecordSortController rsc = dataDefController.getRecordSortController();
if (rsc.canSortOn(sortField))
{
ISort newSort = new Sort();
newSort.setSortField(sortField);
newSort.setDirection(SortDirection.ascendingOrder);
rsc.add(-1, newSort);
}
}