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:
String
.
Sorting
and indexing
data are closely related. See "Sorting and indexing" for further discussion of indexes.
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.
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.
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
DataSet
.
DataSet
.
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:
QueryDataSet
in the content pane.
sort
property. This displays the sort
property editor.
If you selected the wrong column, click the Remove From Sort button, and redo the previous step.
The dialog will look like this:
Click the OK button.
The property values you specify in this dialog are stored in a SortDescriptor
object.
Select Run|Run Project to compile and run the application. It will look like this:
Check the Unique option to create a unique index, which enables a "constraint" on the data in the StorageDataSet
- only rows with unique values for the columns defined as sortKeys
in the SortDescriptor
can be added or updated in a DataSet
.
What is a unique index?
Unique is a constraint on the data set, not just on the index. If you define a unique index on a column, you are asserting that no two rows in the data set have the same value in that column. If there are two or more rows in the data set that have the same value in the unique column when the index is first created, any duplicate rows are moved to another "duplicates" data set.
How this works: when the unique sort
property is applied for the first time, rows that violate the unique constraint are copied into a separate DataSet
. You can access this DataSet
by calling the StorageDataSet.getDuplicates()
method. The duplicates DataSet
can be deleted by calling the StorageDataSet.deleteDuplicates()
method.
You can have one or more unique sort
property settings for a StorageDataSet
at one time. If a duplicates DataSet
exists from a previous unique sort
property setting, additional unique sort
property settings cannot be made until the earlier duplicates have been deleted. This is done to protect you from eliminating valuable rows due to an erroneous unique sort
property setting.
If a unique index is sorted on more than one column, the constraint applies to all the columns taken together: two rows can have the same value in a single sort column, but no row can have the same value as another row in every sort column.
The unique option is useful when you're querying data from a server table that has a unique index. Before the user begins editing the data set, you can define a unique index on the columns that are indexed on the server knowing that there will not be any duplicates. This ensures that the user cannot create rows that would be rejected as duplicates when the changes are saved back to the server.
Index Name
Enter a name in this field to create a named index. This is the user-specified name to be associated with the sort specification (index) being defined in the dialog.
What is a named index?
The intent of the index name is to let you revert to a previously
defined sort. The index has been "maintained" (kept up-to-date), so
that it can be re-used. And in fact, if you set a
data set's sort
property to a new sortDescriptor
with exactly the same
parameters as an existing sort, the existing sort is used.
To view a data set in the order defined by an existing named index, set its sort
property using the sortDescriptor
constructor that takes just an index name.
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);