1 Introduction

Putting Data into Sorted Order

The SORT BY statement allows you to specify a sort using up to ten data items from any data set in the thread. If a sequence of two elements cannot be determined on the basis of the first field, the second, the third, and so on, will be compared until a sequence can be found. If no sequence is found, the pointers into the database are compared in order to determine sequence. Additionally, sort direction is specifiable on each sort field on an individual basis. Any field may be suffixed by the keyword DES to cause the sort to be in descending order rather than ascending.

Here is the same program shown earlier, but with some additional statements filled in:

In this example, lines 80 thru 100 are used to create a file, ASSIGN it to a file position and convert it into a workfile. (Note that the file is still of type DATA.) Line 120 produces pointers so the data can be accessed in sorted order. Line 170 reads the pointer into an Eloquence variable so it can be used in the direct mode DBGET in line 180.

One additional function has been introduced in this example, the WFLEN function used in line 130. This function returns the number of pointers in the workfile. It has as an argument, the file number of the workfile, since more than one workfile may be in use at a given time. Notice that the program creates and purges the workfile each time the program is run. If disk space is available, program execution time can be decreased by deleting lines 80 and 220, which allows the file to remain on the disk.

   10    DIM Buf$[170],B$[5],Order_no$[10],Name$[30]
   20    INTEGER S(9)                  !  Ten-element status array.
   30    B$="  SAD"
   40    DBOPEN (B$,"MANAGER",3,S(*))  !  Open the database.
   50    DBASE IS B$
   60    IN DATA SET "CUSTOMER" USE Order_no$,Name$
   70    !  Now set up a workfile with CUSTOMER as the thread.
   80    FCREATE "XYZ" ,0
   90    ASSIGN "XYZ" TO #1
   100   WORKFILE IS #1;THREAD IS "CUSTOMER"
   110   !  Sort the orders by order number.
   120   SORT BY Order_no$
   130   Entry_count=WFLEN(1)   !WFLEN returns no of pointers in file.
   140   PRINT " ORDER NUMBER";TAB(30);"CUSTOMER NAME";LIN(2)
   150   FOR I=1 TO Entry_count
   160   !  Read the record pointer into Rec_no.
   170   READ #1;Rec_no
   180   DBGET (B$,"CUSTOMER",4,S(*),"@",Buf$,Rec_no)
   190   PRINT Order_no$:TAB(30);Name$
   200   NEXT I
   210   DBCLOSE (B$," ",1,S(*))
   220   PURGE "XYZ"
   230   END

Eloquence Sort Manual - 19 DEC 2002