To add a new sorting definition

New sorting definitions are added to a report with the com.crystaldecisions.sdk.occa.report.application.RecordSortController class. A sorting definition is defined by a field and a direction, however, not all fields can be grouped on. The canSortOn method of the RecordSortController class checks whether the field can be sorted on.
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.
  1. Retrieve the DataDefController object.
    DataDefController dataDefController = rcd.getDataDefController();
  2. Find the 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 Last_Years_Sales field called from a table called Customer.
    Fields<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
    IField sortField = resultFields.findField("{Customer.Last_Years_Sales}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
  3. Retrieve the RecordSortController object.
    RecordSortController recordSortController = dataDefController.getRecordSortController();
  4. 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);
  5. Call the add method of the SortController 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 0 indicates that the new sorting definition will be applied to the beginning of the Sorts collection and will be applied to records before any other sorting definitions are applied. A value of -1 indicates that the new sorting definition will be added to the end of the Sorts collection and will be applied to records after all other sorting definitions have been applied.
    recordSortController.add(0, newSort);
Example: 
The following code adds a new sorting definition to a report based on the field Customer.Last_Years_Sales. 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<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
  IField sortField = resultFields.findField("{Customer.Last_Years_Sales}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
        
  RecordSortController recordSortController = dataDefController.getRecordSortController();
  if(RecordSortController.canSortOn(sortField))
  {
    ISort newSort = new Sort();
    newSort.setSortField(sortField);
    newSort.setDirection(SortDirection.ascendingOrder);
    recordSortController.add(0, newSort);
  }  
}
This list includes the classes used by the sample code:
  • com.crystaldecisions.sdk.occa.report.application.DataDefController
  • com.crystaldecisions.sdk.occa.report.application.RecordSortController
  • com.crystaldecisions.sdk.occa.report.application.ReportClientDocument
  • com.crystaldecisions.sdk.occa.report.data.FieldDisplayNameType
  • com.crystaldecisions.sdk.occa.report.data.Fields
  • com.crystaldecisions.sdk.occa.report.data.IField
  • com.crystaldecisions.sdk.occa.report.data.ISort
  • com.crystaldecisions.sdk.occa.report.data.Sort
  • com.crystaldecisions.sdk.occa.report.data.SortDirection
  • com.crystaldecisions.sdk.occa.report.lib.ReportSDKException