Custom sort crystal reports

Being able to sort records in either ascending or descending order is a fundamental reporting skill. Sorting makes it easy for a user to quickly find a particular piece of data buried within a large report. This chapter has ten tutorials that quickly get you up to speed on every aspect of sorting and grouping data.

Become a Crystal Reports expert with the authoritative resource available. The tuturials and tips in this book will take your skills to the next level.
Buy at

This is an excerpt from the book Crystal Reports Encyclopedia. Click to read more chapter excerpts.

Tutorial 3-7. Custom Group Sorting

When creating a group, you have to specify the order to sort the data in (the default is ascending order). To make a report more flexible, you can let the user choose how to sort the report. For example, a person would want a report sorted in descending order by date if they want to see the most recent activity. But someone performing an audit would want to sort the date in ascending order so that they can see historical data first. Crystal Reports allows dynamic sorting by letting you specify a formula for the group sorting order.

Note
Dynamic sorting is only allowed on group fields. Crystal Reports doesn't allow modifying the sort order of detail records. How unfortunate!
  1. Open the report Sales by Country and Product Type.rpt that you created in the previous example.
  2. Rename it to Sales with Custom Sorting.rpt.
  3. Create a new parameter called Product Type Sort Order. This parameter prompts the user to sort the Product Type group in either ascending or descending order. The following screen shot shows how to set the parameter settings.
  4. Now we want to modify the group sorting so that it uses a formula to determine the sorting order. Select Report | Group Expert to open the dialog box.
  5. Select the Product Type field and click the Options button. This opens the Change Group Options dialog box.
  6. Click the checkbox Use a Formula as Group Sort Order. This enables the Formula Workshop button.
  7. Click on the Formula Workshop button and enter the following formula: If = "Ascending" Then crAscendingOrder Else crDescendingOrder This formula checks the value of the parameter and sets the sort order accordingly. Notice how the sort order uses the constants crAscendingOrder or crDescendingOrder. These are predefined constants by Crystal Reports.
  8. Preview the report and you'll see that the Product Type sort order is based upon the parameter value.


To read all my books online, click here for the Crystal Reports ebooks.