To add a Top N sorting definition

Before you can add a Top N sort to a report, it must contain a summary field that summarizes grouped data.
You can use a Top N sort to select groups with the highest summarized values. For example, if you have a report that is grouped by country and summarizes sales for each country, you can use a Top N sort to select the top five countries with the highest sales figures. You can also use Top N sorts to select groups with the lowest summarized values, or to select the top or bottom groups by percentage.
  1. Retrieve the DataDefController object.
    DataDefController dataDefController = rcd.getDataDefController()
  2. Retrieve a summary field that summarizes grouped data.
    IField summaryField = dataDefController.getDataDefinition().getSummaryFields().getField(0);
  3. Retrieve the GroupSortController object.
    GroupSortController groupSortController = dataDefController.getGroupSortController();
  4. Create a new TopNSort object and set the summary field to sort on with the setSortField method.
    TopNSort topNSort = new TopNSort();
    topNSort.setSortField(summaryField);
  5. Set the direction of the sort with the setDirection method.
    This example sorts the groups by the highest N values.
    topNSort.setDirection(SortDirection.topNOrder);
  6. Set the number of groups in the Top N.
    In this example, the top five groups are considered.
    topNSort.setNIndividualGroups(5);
  7. Specify how to handle groups that are not in the Top N.
    This example combines the remaining groups into a single group called "Others".
    topNSort.setDiscardOthers(false);
    topNSort.setNotInTopBottomName("Others");
  8. Call the addSummarySort method of the GroupSortController class to add the new group to the report based on the chosen field.
    The first parameter of the addSummarySort 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 last after all other sorting definitions have been applied.
    groupSortController().addSummarySort(-1, topNSort);
Example: 
The following code adds a new top five sort to a report based on a summary field that summarizes grouped data.
void addTopN(ReportClientDocument rcd) throws ReportSDKException
{
  DataDefController dataDefController = rcd.getDataDefController();
  IField summaryField = dataDefController.getDataDefinition().getSummaryFields().getField(0);
        
  GroupSortController groupSortController = dataDefController.getGroupSortController();
        
  TopNSort topNSort = new TopNSort();
  topNSort.setSortField(summaryField);
  topNSort.setDirection(SortDirection.topNOrder);
        
  topNSort.setNIndividualGroups(5);
  topNSort.setDiscardOthers(false);    
  topNSort.setNotInTopBottomName("Others");
       
  groupSortController.addSummarySort(-1, topNSort);
}
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.IField
  • com.crystaldecisions.sdk.occa.report.data.ITopNSort
  • com.crystaldecisions.sdk.occa.report.data.SortDirection
  • com.crystaldecisions.sdk.occa.report.data.TopNSort
  • com.crystaldecisions.sdk.occa.report.lib.ReportSDKException