10 REM SORT BOWLER HEADER RECORDS ALPHABETICALLY BY NAME   (SORTFILE)
12 REM Developed by William W. Klaproth,  Moraga Ca.  94556    September 1982
20 REM MASTER FILE DESCRIPTIONS FOR ALL BOWLING PROGRAMS
30 OPEN "B:TEAMS.HDR" AS #1 LEN = 64
40 FIELD #1, 2 AS TM$, 30 AS NAM$, 2 AS RCNT$, 2 AS GAM$, 4 AS PINS$, 2 AS AVG$, 2 AS HIG$, 2 AS HIS$, 4 AS PTSW$, 4 AS PTSL$, 2 AS B1ID$, 2 AS B2ID$, 2 AS B3ID$, 2 AS B4ID$, 2 AS B5ID$
50 FIELD #1, 2 AS HDRID$, 30 AS LEAGUE$, 2 AS WEEKS$, 2 AS TEAMS$, 4 AS FILLER$, 2 AS BWLPERTM$, 2 AS NXTID$, 2 AS HCPBASE$, 4 AS HCPPCNT$
60 OPEN "B:BOWLERS.HDR" AS #2 LEN = 64
70 FIELD #2, 2 AS BBO$, 30 AS BNAM$, 2 AS BRCNT$, 2 AS BGAM$, 4 AS BPINS$, 2 AS BAVG$, 2 AS BHIG$, 2 AS BHIS$, 2 AS BHCP$, 2 AS BHIGH$, 2 AS BHISH$, 2 AS BTM$,2 AS GAM2$, 2 AS SER5$, 2 AS SER6$,2 AS CURSER$,2 AS AVGLSTYR$
80 REM OPEN "B:TEAM.DET" AS #3 LEN = 16
90 REM FIELD #3,2 AS DTM$, 2 AS TMO$, 2 AS TDAY$, 2 AS TG1$, 2 AS TG2$, 2 AS TG3$, 2 AS TSER$, 2 AS DPTSW$
100 REM OPEN "B:BOWLER.DET" AS #4 LEN = 16
110 REM FIELD #4,2 AS DBO$, 2 AS BMO$, 2 AS BDAY$, 2 AS BG1$, 2 AS BG2$, 2 AS BG3$, 2 AS BSER$
120 CLS
130 PRINT "Sort and Print Bowler averages in name sequence"
140 GET #1,1
150 BOWLERS% = CVI(NXTID$)-1   'STORE NUMBER OF BOWLERS IN LEAGUE
160 DIM TNAM$(BOWLERS%)
170 DIM TBO%(BOWLERS%)
180 REM LOAD SORT TABLE WITH BOWLER NAMES AND RECORD NUMBERS
190 PRINT "LOADING SORT TABLE WITH BOWLER NAMES"
200 FOR A% = 1 TO BOWLERS%
210 GET #2,A%
220 GOSUB 590            'REVERSE FIRST AND LAST NAMES
230 TNAM$(A%) = HNAM$
240 TBO%(A%) = CVI(BBO$)
250 NEXT A%
260 REM BEGIN SORT PHASE
270 PRINT "SORTING BOWLER HEADER RECORDS"
280 M% = BOWLERS%: LAST$ = " "
290 M% = M% / 2
300 IF LAST$ = "Y" GOTO 440
310 IF M% = 1 THEN LAST$ = "Y"
320 K% = BOWLERS% - M%: J% = 1
330 I% = J%
340 L% = I% + M%
350 IF TNAM$(I%) <= TNAM$(L%) GOTO 410
360 TEMPN$ = TNAM$(I%): TEMPB% = TBO%(I%)
370 TNAM$(I%) = TNAM$(L%): TBO%(I%) = TBO%(L%)
380 TNAM$(L%) = TEMPN$: TBO%(L%) = TEMPB%
390 I% = I% - M%
400 IF I% > 0 GOTO 340
410 J% = J% + 1
420 IF J% > K% GOTO 290 ELSE GOTO 330
430 REM BEGIN OUTPUT PHASE
440 PRINT "END OF SORT PHASE"
450 LPRINT "NUM BOWLER NAME           PINS GAM AVG"
460 LPRINT "--- -----------           ---- --- ---"
470 FOR A% = 1 TO BOWLERS%
480 GET #2,TBO%(A%)
490 LPRINT USING " ## ";CVI(BBO$);
500 REM LPRINT LEFT$(BNAM$,20);'REPLACES NEXT LINE IF REVERSED NAME NOT DESIRED
510 LPRINT TNAM$(A%);
520 LPRINT USING " ##### ";CVS(BPINS$);
530 LPRINT USING "### ";CVI(BGAM$);
540 LPRINT USING "###";CVI(BAVG$)
550 NEXT A%
560 PRINT "Processing Completed - Returning to MENU"
570 CLOSE
580 RUN "A:MENU"
590 REM ROUTINE TO REVERSE FIRST AND LAST NAMES
600 HNAM$ = LEFT$(BNAM$,20)
610 FOR C% = 1 TO 20
620 CP$ = MID$(HNAM$,(21-C%),1)
630 IF CP$ = " " GOTO 640 ELSE GOTO 650
640 NEXT C%
650 ENDNAM% = C%
660 FOR D% = C% TO 20
670 CP1$ = MID$(HNAM$,(21-D%),1)
680 IF CP1$ = " " GOTO 700
690 NEXT D%
700 BEGNAM% = D%
710 LSTNAM$ = MID$(HNAM$,(22-D%),(D%-C%))
720 FSTNAM$ = LEFT$(HNAM$,(20-D%))
730 MID$(HNAM$,1,(D%-C%)) = LSTNAM$
740 MID$(HNAM$,(D%-C%+1),2) = ", "
750 MID$(HNAM$,(D%-C%+3),(20-D%)) = FSTNAM$
760 RETURN