To modify a group sort

Group sorts are added automatically when a group is added. You cannot add or remove a group sort independently from a group. However, you can modify the group sort, for example, to change the sort direction. This example assumes that the report contains a group based on the Customer.Country field.
  1. Retrieve the DataDefController object.
    DataDefController dataDefController = rcd.getDataDefController();
  2. Find a field that the report is grouped and sorted on.
    The IDataDefinition.getResultFields method returns all fields that have been added to the report design.
    Fields<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
    IField sortField = resultFields.findField("{CUSTOMER.COUNTRY}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);
  3. Retrieve the GroupSortController object.
    GroupSortController groupSortController = dataDefController.getGroupSortController();
  4. Retrieve the ISort object for the group.
    ISort oldSort = groupSortController.findSort(sortField);
  5. Create a new ISort object by cloning the old ISort object, and setting the direction of the sort.
    ISort newSort = (ISort) oldSort.clone(true);
    newSort.setDirection(SortDirection.descendingOrder);
  6. Call the modify method of the GroupSortController class to add the new sort to the report based on the chosen field.
    The first parameter of the modify method specifies the original sorting definition to be modified. The second parameter specifies the new sorting definition.
    groupSortController.modify(oldSort, newSort);
Example: 
The following code modifies a report that contains a group based on the Customer.Country field. It changes the sort direction of the group to descending order.
void modifyGroupSortDirection(ReportClientDocument rcd) throws ReportSDKException
{
  DataDefController dataDefController = rcd.getDataDefController();

  Fields<IField> resultFields = dataDefController.getDataDefinition().getResultFields();
  IField sortField = resultFields.findField("{CUSTOMER.COUNTRY}", FieldDisplayNameType.formulaName, java.util.Locale.ENGLISH);

  GroupSortController groupSortController = dataDefController.getGroupSortController();
  ISort oldSort = groupSortController.findSort(sortField);

  if (groupSortController.canSortOn(sortField))
  {
    ISort newSort = (ISort) oldSort.clone(true);
    newSort.setDirection(SortDirection.descendingOrder);
    groupSortController.modify(oldSort, newSort);
  }
}
This list includes the classes used by the sample code:
  • com.crystaldecisions.sdk.occa.report.application.DataDefController
  • com.crystaldecisions.sdk.occa.report.application.GroupSortController
  • 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