SAP Knowledge Base Article - Public

1211558 - How to dynamically change the sort direction of a field on a report in Crystal Reports?

Symptom

  • How to change how a report is sorted?
  • How can you dynamically sort report records in ascending or descending order in Crystal Reports?

Environment

  • SAP Crystal Reports 2011
  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016

Resolution

  • To dynamically sort a database field in ascending order, or desending order, on a report in Crystal Reports, follow the general steps below:
          
    1. Create a parameter with 2 default values in the picklist: Descending and Ascending.
      • 1.1 On the 'Insert' menu, click 'Parameter', then click 'New'.
      • 1.2 Type the name and prompting text for the parameter in the corresponding boxes.
      • 1.3 In the 'Type' box, click 'String'.
      • 1.4 Click 'Set Default'.
      • 1.5 In the 'Value to add' box, type 'Descending' then click '>' to move it to the 'Default Values' list.
      • 1.6 Repeat the above step for 'Ascending'.
      • 1.7 Click 'OK', then click 'OK' again to return to the report.
            
    2. Create 2 formulas based on the field you want to change the sort direction on:
      • 2.1 On the 'Insert' menu, click 'Formula', then click 'New'.
      • 2.2 Create this formula: @Ascending
           
               If {?Parameter} = "Ascending" then {DatabaseField}
           
      • 2.3 Save this formula and close the Formula Editor. This brings you back to the Formula tab of the Insert Fields dialog box.
      • 2.4 Click 'New' and create this formula: @Descending
          
              If {?Parameter} = "Descending" then {DatabaseField}
            
      • 2.5 Save this formula and close the Formula Editor.
      • 2.6 Click 'Close' to return to the report.
            
    3. Sort the report based on these two formulas.
      • 3.1 On the 'Report' menu, click 'Sort Records'.
      • 3.2 In the 'Report Fields' list, click {@Ascending} and move it to the 'Sort Fields' list.
      • 3.3 Under 'Sort Direction', click 'Ascending'.
      • 3.4 In the 'Report Fields' list, click {@Descending} and move it to the 'Sort Fields' list.
      • 3.5 Under 'Sort Direction', click 'Descending'.
      • 3.6 Click 'OK' to return to the report.
                   
    4. Once you preview the report you will be prompted to select a parameter value, which will determine how the report is sorted.

Keywords

SORTING DYNAMIC ORDER DESCENDING ASCENDING Seagate Crystal Reports Sorting records Using parameters and formulas Crystal Reports 5 or higher , c2006057, CR , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , How To

Product

SAP Crystal Reports 2011 ; SAP Crystal Reports 2013 ; SAP Crystal Reports 2016