4D S.A. Copyright 2001

This section describes how to search and sort records.

Searching


Searching is one of the most frequently performed database operations. A search is often the most convenient way to select the records with which you want to work.

Using the 4D Open for Java methods, you can search for records in a table or for records in the current selection. In either case, the search is built in the same way—the only difference is the scope of the search.

Creating a Search Condition

The term "searching" refers to finding a group of records in the database based on the contents of one or more fields. A search definition is a set of instructions that tells 4D Server which records to include in the new current selection.

A search definition indicates which table is going to have its selection changed and includes one or more search conditions. A search condition always contains five elements: logical operator, table number, field number, comparison operator, and value.

The logical operator indicates how the search condition is related to previous search conditions. If it is the first search condition, the logical operator must be equal to 0 or NONE.

The table number and field number identify the field to be used in the search and the table that contains the field.

The comparison operator indicates how to compare the contents of the field to the value you specify (equal to, greater than, less than, and so forth).

The value specifies the number, string, or other value to which each record is compared.

Search Conditions in 4D Client Query Editor

In 4D Client, the user can specify search conditions in the Query editor. The following graphic shows 4D Client Query editor and indicates which portions of the Query conditions correspond to the elements in a 4D Open for Java search condition.

Sorting Records


A sort reorders the records in a selection according to the values in the database.

Remember that 4D Server reorders the references of the selection in memory, not the actual records on disk. It is common to sort records:

To view records on screen in a particular order

Before printing a report

Before graphing data

When you first enter data into a new database, 4D Server stores the records in the order in which they are entered or imported. When you display or print records, they appear in this order. Often, you want to view records in another order. For example, you might want to alphabetize a list of names in a report.

If you are sorting on only one field which is indexed, 4D Server uses the index to perform the sort. If you are sorting on a non-indexed field or doing a multi-level sort, the sort is sequential.

Creating a Sort Condition

A sort definition indicates which table will have its selection sorted and includes one or more sort levels that indicate how the records are to be sorted. A sort level always contains three elements: table number, field number, and sorting order.

Table Number and Field Number

The table number and field number specify the field on which the sort should be conducted. If you specify an indexed field in the sort criteria, the sort will be indexed. Otherwise, it will be sequential.

Sorting Order

The sorting order specifies whether the sort should be ascending or descending. You can specify either an ascending or descending order for each field on which you are sorting. Sorting from A to Z or smallest to largest is known as an ascending sort. Sorting in the reverse order is called a descending sort—largest to smallest, latest to earliest, and Z to A. If you are sorting on more than one level, you can freely mix ascending and descending sort orders.

Sorting Order Value
Ascending 1
Descending 0

Suppose that you want to sort a selection of Customers by name in ascending order. Given that CustomerName is the first field in the second table, the sort definition would appear as follows:

2 1 1
Second table First field In ascending order

Sort Conditions in 4D Client Order Editor

In 4D Client, the user specifies sort levels in the Order editor. The following graphic shows some sample sort lines in 4D Client Order editor and indicates how they correspond to the elements in a 4D Open for Java sort level.


4D S.A. Copyright 2001