Progress
Language Tutorial
for Windows
Sorting Records with the BY Phrase
You use the BY option to sort by a field that is not indexed. The other sort options work efficiently with indexed fields, but work less efficiently with non-indexed fields. BY can work with either, which makes it the most useful sorting option.
There are three basic techniques to sorting with BY:
The simplest technique is to supply a database field name as this code example shows:
The first example selects all United States-based companies and sorts the records by postal code, which is the ZIP code. This code could be used with an envelope-printing procedure to meet the sorting requirements of the U. S. Postal Service.
You can use multiple BY phrases in the same query as this example shows:
The first BY option is the primary sort order, and the second is the secondary sort order, and so on. In other words, this code first sorts the customers by their base country. Records within each country group are then sorted by postal code. With just a little more code, you’ve internationalized your envelope application.
Using Expressions with BY
The BY option can accept more than just database fields. You can also create expressions based on fields and sort by the results. See "Working with Expressions," for more information on valid expressions.
For example, suppose you need to code a browse widget to show the insurance charge for shipping a particular item. Insurance costs either $1 or 1% of the list price, whichever is higher. You can calculate the insurance based on the Price database field. If you sort the records in the browse descending by the insurance charge, then the sales representatives can quickly see which items have an insurance charge of more than $1.
Follow these steps to view the sorting strategy previously discussed:
- Open
lt-09-02.p
and run it. The following display appears:
![]()
As you can see, for each record, the procedure calculated the cost of shipping insurance. Since this is a descending list, you can quickly pick out which items cost more than $1 to insure.
- Choose Exit, then press SPACEBAR to return to the Procedure Editor.
Here is the code that created the display:
NOTE: The THREE-D option is relevant only on a Windows client; it is ignored by a character client.These notes help explain the code:
- In the widget definition statement, you include space for a calculated field (Price / 100), which is a temporary new field based on the value of other fields.
- In the OPEN QUERY statement, you use the expression that corresponds to the calculated field in order to sort the calculated field. The descending option places those products with higher insurance charges at the top of the list.
Copyright © 2004 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |