Sorting data

Database application development is a feature of JBuilder Professional and Enterprise. Distributed application development is a feature of JBuilder Enterprise.

Sorting a data set defines an index that allows the data to be displayed in a sorted order without actually reordering the rows in the table on the server.

Data sets can be sorted on one or more columns. When a sort is defined on more than one column, the dataset is sorted as follows:

You can sort the data in any DataSet subclass, including the QueryDataSet, ProcedureDataSet, TableDataSet, and DataSetView components. When sorting data in JBuilder, note that:

Sorting and indexing data are closely related. See "Sorting and indexing" for further discussion of indexes.

Sorting data in a JdbTable

If your application includes a JdbTable that is associated with a DataSet, you can sort on a single column in the table by clicking the column header in the running application. Click again to toggle from ascending to descending order.

Click on header to sort

When sorting data in this way, you can only sort on a single column. Clicking a different column header replaces the current sort with a new sort on the column just selected.

Sorting data using the JBuilder visual design tools

If you need your application to sort in a specified order, the JBuilder visual design tools allow you to quickly set these properties. The DataSet sort property provides an easy way to

This example describes how to sort a data set in ascending order by last name. To set sort properties using the JBuilder visual design tools:

  1. Open or create the project from "Retrieving data for the tutorials".
  2. Click the Design tab. Select the QueryDataSet in the content pane.
  3. In the Inspector, select, then double-click the area beside the sort property. This displays the sort property editor.
  4. Specify values for options that affect the sort order of the data. In this case, select the LAST_NAME field from the list of Available Columns, click Add To Sort.
  5. If you selected the wrong column, click the Remove From Sort button, and redo the previous step.

    The dialog will look like this:

  6. Click the OK button.

    The property values you specify in this dialog are stored in a SortDescriptor object.

  7. Select Run|Run Project to compile and run the application. It will look like this:

Understanding sorting and indexing

There are two options on the Sort dialog that benefit from further discussion: Unique and Index Name. Sorting and indexing are closely related. The following describes the unique option and named indexes in more detail.

Sorting data in code

You can enter the code manually or use JBuilder design tools to generate the code for you to instantiate a SortDescriptor. The code generated automatically by the JBuilder design tools looks like the following:


queryDataSet1.setSort(new com.borland.dx.dataset.SortDescriptor("",
     new String[] {"LAST_NAME", "FIRST_NAME", "EMP_NO"}, new boolean[]
     {false, false, false,  }, true, false, null));

In this code segment, the sortDescriptor is instantiated with sort column of the last and first names fields (LAST_NAME and FIRST_NAME), then the employee number field (EMP_NO) is used as a tie-breaker in the event two employees have the same name. The sort is case insensitive, and in ascending order.

To revert to a view of unsorted data, close the data set, and set the setSort method to null, as follows. The data will then be displayed in the order in which it was added to the table.

queryDataSet1.setSort(null);