### Sorting the data

By default, the records in a table are displayed randomly; they are displayed in the order they are returned from the fetch operation. You can specify that JReport sort the records in a table, and also within the groups in table, if any.

The following example shows how to set the sorting criteria in a table.

Suppose that you have created a table based on the query EmployeeInformation in the catalog file SampleReports.cat as follows: added the fields Hire Date, Name, Home Phone and Salary to be displayed in the table, set the field Employee Position as the group by field and Ascend as the sort order, then applied the default style to the table. See the table.

For the example, specify the sorting criteria as below:

1. Select the table and do one of the following:
   - Click Insert > Group on the menu bar.
   - Click the Insert Group button on the Insert toolbar.
   - Right-click the table and select Table Wizard from the shortcut menu.
2. In the Table Wizard, click the Display screen, then click the Sort Fields by button.
3. In the Sort Fields by dialog, select the field Hire Date as the sort by field from the Resources panel and click to add it to the sort by panel, select Ascend from the Sort column, then click OK to accept the changes.
4. Click Finish in the Table Wizard to apply the settings.
5. View the table again and you will find that the records in each group are displayed in ascending order by Hire Date. See the result.

#### Sorting data dynamically

Similar to dynamic grouping, sorting can also be defined at runtime. Using the table in the above example, you can apply a dynamic sort on it as follows:

1. Right-click the table and select Table Wizard from the shortcut menu.
2. In the Table Wizard, click the Display screen, then click the Sort Fields by button.
3. In the Sort Fields by dialog, remove the sort by field Hire Date set in the above example.
4. In the Resources panel, click <Add Parameter...> in the Parameters node.
5. In the Create Parameter dialog, create a type-in parameter named SortBy of String type (leave the other settings to their default), then click OK.
6. Add the just created parameter SortBy as the sort by field.
7. Click Dynamic Sort in the sort order drop-down list of the Sort column.
8. In the Specify Sort Order for dialog, input SortBy Order, then click OK to close the dialog.
9. Click OK in the Sort Fields by dialog to accept the changes.
10. Click Finish in the Table Wizard to apply the settings.
11. View the report result. In the Enter Parameter Values dialog, select a field from the SortBy drop-down list and specify the sorting manner in the SortBy Order drop-down list. See illustration.

    Here, we choose Salary from the SortBy list and DESCENDING from the SortBy Order list. The records within each group are displayed in descending order according to their salary values. See the result.