SORTING BY FIELDS
Rick Keck
Overland Park, KS
For Apple, PETICBM, VIC – this ripple sort will sort records using any internal location as its key. For example, R. J. Brown can be alphebetized starting at the "B" in Brown and ignoring "R. J."
Occasionally computer users need to sort data in a special way. Several sort routines are available, however most do not allow the flexibility of sorting data by fields. The program with this article illustrates a different type of application for a sort routine. The sorting algorithm used in this example is a ripple sort. The code as shown will work on either a Commodore or an Apple computer. Specifically, this program demonstrates a sorting routine which allows sorting of a file of string records by a user-specified field.
Observe the ten data statements at the top of the program noting that each record consists of three fields of data. It is essential that these fields start at a specified column in each of the records so that the file is consistent in its construction. In this case, the following fields start at the stated columns in each record in the file.
Field (1): | Name - column 1 |
Field (2): | Number - column 11 |
Field (3): | Code - column 21 |
With this program the user responds to a computer request by stating which column position the file of records is to be sorted by. The important factors which contribute to the ability to sort the file by a field are as follows: First, each record consists of a large, single string of data. Second, the utilization of the MID$ function in the sorting section of the program allows comparison of a substring of each record.
This sorting program can be made into a subroutine and inserted into an existing program by doing the following. Delete lines 10 through 230; renumber the code as desired; replace the END statement with a RETURN statement; and call the subroutine with a GOSUB statement. Note that the variable N must be assigned the value of the number of records in the file to be sorted. The variable C$ is a variable string array with each element holding a record. This array must be dimensioned to at least size N. The variable B specifies the length of the field which will be sorted. In this code example it is set to the value of six. Since the data consists of a file of character string records, it is suggested that the data be sorted in the form of a sequential data file on external storage devices.
This sorting program can be used in a variety of applications. For example, sorting addresses by zip code, sorting transactions by account number, or sorting records by a date field.
90 N = 10 : REM N IS # OF RECORDS 100 DIM C$ (N) 110 DATA "RICHIE 231105 COOL4" 120 DATA "PAT 250421 BASE9" 130 DATA "TRENT 200818 FARM1" 140 DATA "TRIXIE 222222 KITY3" 150 DATA "ERIC 154210 HSIF8" 160 DATA "ANGIE 021356 SYOB3" 170 DATA "DARRON 312540 DIK12" 180 DATA "TINKER 312450 TIGR7" 190 DATA "THEO 110055 CAT28" 200 DATA "JAK 003451 ACCT5" 210 FOR J = l TO N 220 READ C$ (J) 230 NEXT J 240 PRINT:PRINT : PRINT 250 PRINT "1234567890…5…20…5…30" 255 PRINT 260 PRINTC$ (1) 270 PRINT : PRINT "ENTER THE COLUMN # OF THE FIELD" 280 PRINT : PRINT "THAT THE FILE IS TO BE SO RTED BY"; 290 INPUT A 295 PRINT : PRINT 300 REM SET THE LENGTH OF THE FIELD 310 REM TO THE VALUE OF (6) 320 B = 6 330 REM ****** SORT BEGINS ****** 340 FOR J = l TO N - l 350 IF (MID$ (C$ (J), A, B) < MID$ (C$ (J + l), A, B)) THEN 420 360 T$ = C$ (J + 1) 370 FOR K = J TO 1 STEP - 1 380 IF (MID$ (C$ (K), A, B) < MID$ (T$, A, B)) THEN C$ (K + l) = T$ : G0T0 420 390 C$ (K + 1) = C$ (K) 400 NEXT K 410 C$ (1) = T$ 420 NEXT J 430 REM ****** SORT ENDS ****** 440 FOR J = l TO N 450 PRINT C$ (J) 460 NEXT J 470 PRINT : PRINT "NORMAL TERMINATION" 480 END