How to: Display Records in Descending Order
Visual FoxPro displays records in a table in ascending order by default. However, you can create indexes that display records in descending order or you can display records in descending order when you view records in a table.
Note
You cannot change the order for displaying or processing records when using binary indexes.
You can create descending indexes in structural compound index (.cdx) files using the Visual FoxPro IDE or the language. To create descending indexes for other index files, use the Visual FoxPro language.
To create indexes in descending order
Open the Table Designer to modify your table and choose the Indexes tab.
In the Order column next to the Name box for the index, click the upward-pointing arrow so that it points down.
Note
An upward-pointing arrow appears in the Order column only when an index name exists in the Name box.
To display records in descending order programmatically
Include the DESCENDING keyword in the following Visual FoxPro commands:
Though Visual FoxPro displays records in ascending order by default, these commands also include the ASCENDING keyword. You can use both keywords to change orders based on the order that is most convenient.
For example, the following code opens the Products table in the Visual FoxPro sample database, TestData, using the USE command. The INDEX command creates an index based on the Unit_Price field with the index name, or tag, Unit_Price. The INDEX command includes the DESCENDING keyword to display the records in descending order in a browse window when the BROWSE command is called. The records in the Products table appear in order of highest price to lowest price:
OPEN DATABASE (HOME(2) + 'Data\TestData')
USE Products
INDEX ON Unit_Price TAG Unit_Price DESCENDING
BROWSE
When an index already exists, you can view records in descending order instead of creating a new descending index. For example, suppose you created an index that organizes a product table by unit price as shown in the following code:
USE Products
INDEX ON Unit_Price TAG Unit_Price
By default, Visual FoxPro displays records in ascending order by default. However, you can browse records in descending order using the following code:
USE Products
SET ORDER TO Unit_Price DESCENDING
BROWSE
See Also
Tasks
How to: Create Indexes (Visual FoxPro)
How to: Set Controlling Indexes