10 'SORTC - A VARIABLE LENGTH, VARIABLE COLMN, AND UNLIMITED
20 'FILE SIZE, SORT PROGRAM - MERGE.BAS IS CALLED TO MERGE
30 'STRINGS CREATED BY THIS SORT.
40 ' IMPROVEMENTS:
50 '	VARIABLE COLUMNS FOR SORT BY INPUT STATEMENT
60 '	RESORT OPTION AFTER MATRIX IS LOADED AND AFTER
70 '	  FIRST SORT IS COMPLETED.
80 '	ADDITIONAL FILES OPTIONAL BEFOR TERMINATION.
90 GOSUB 660
100 RESET
110 CLEAR 2500
120 DEFINT I-N,A,B
130 FCNT=100
140 STR=200
150 DIM A$(500)
160 '
170 PRINT
180 PRINT "Enter file names in upper case-letters."
190 PRINT "FILE NAMES NEED NOT BE EMBEDDED IN QUOTES."
200 PRINT
210 INPUT "WHAT IS THE  NAME OF THE ORIGINAL FILE";OLD$
220 INPUT "WHAT NAME DO YOU WANT FOR THE NEW FILE";NW$
230 '
240 OPEN "I",#2,OLD$:I=0
250 '
260 IF EOF(2) THEN 320
270 I=I+1
280  LINE INPUT #2,A$(I)
290 IF I>STR-1 THEN GOTO 320
300 GOTO 260
310 '
320 N=I:S=I
330 PRINT: PRINT "   ";N;" RECORDS READ."
340 '
350 ' THIS STARTS THE ACTUAL SORT
360 '
370 GOSUB 660 'CLEAR SCREEN
380 F$="0        1         2         3         4         5         6         7"
390 G$="1234567890123456789012345678901234567890123456789012345678901234567890"
400 IF A<>B THEN GOTO 430
410 PRINT F$:PRINT G$:PRINT A$(8):PRINT
420 INPUT "ENTER SORT PARMS A,B (A=STARTING COL B=LENGTH OF FIELD)";A,B
430 PRINT: PRINT "STARTING SORT ON COL'S ";A;" THRU ";A+B-1
440 J4=N
450 J4=J4\2
460 IF J4=0 THEN 620 'DONE
470 J2=N-J4
480 J=1
490 I=J
500 J3=I+J4
510 '
520 ' SORTING COLUMNS ARE DETERMINED BY  A,B  IN MID$() COMMAND
530 ' THESE VALUES ARE A= STARTING COLUMN B= NUMBER OF COLUMNS.
540 '
550 IF MID$(A$(I),A,B) <= MID$(A$(J3),A,B) THEN 590
560 SWAP A$(I),A$(J3)
570 I=I-J4
580 IF I>=1 THEN 500
590 J=J+1
600 IF J>J2 THEN 450
610 GOTO 490
620 PRINT "STRING SORT COMPLETED "
630 GOSUB 680 'WRITE SORTED FILE TO DISK STRINGS
640 IF EOF(2) THEN GOTO 830
650 GOTO 260
660 PRINT CHR$(27);"*                  "
670 RETURN
680 ' HERE TO DO MULTI STRING SORT (LARGE FILES)
690 '
700 N=S:NT=NT+N
710 FCNT=FCNT+1
720 '
730 NFIL$=MID$(STR$(FCNT),2,3)+".MFL" 'INCREMENTS NUMBER
740 OPEN "O",#3,NFIL$
750 FOR I=1 TO S 'WRITE LOOP
760 PRINT #3,A$(I)
770 NEXT I
780 CLOSE #3
790 FOR I=1 TO STR:A$(I)="":NEXT I:I=0
800 PRINT "STRING ";FCNT-100;" SAVED ";S;" RECORDS"
810 IF EOF(2) THEN IF FCNT=101 THEN GOTO 830
820 RETURN
830 PRINT "STRINGS COMPLETED "
840 PRINT "RECORDS READ = ";NT
850 PRINT "FILES GENERATED = ";FCNT-100
860 N=FCNT
870 CLOSE #1,#2,#3
880 ' STRING MERGE
890 '
900 MFIL1$="101.MFL"
910 MFIL2$="102.MFL"
920 MFIL3$="N101.MFL"
930 MF=1
940 CH=102 'THERE ARE AT LEAST TWO STRINGS
950 '
960 OPEN "I",#1,MFIL1$ '1 ST MERGE FILE
970 OPEN "I",#2,MFIL2$ '2 ND MERGE FILE
980 OPEN "O",#3,MFIL3$ '1 ST MERGE OUTPUT FILE
990 ' START MERGE
1000 '
1010 IF EOF(1) THEN GOTO 1030
1020 LINE INPUT #1,A$
1030 IF EOF(2) THEN GOTO 1050
1040 LINE INPUT #2,B$
1050 IF EOF(1) THEN  IF EOF(2) THEN CLOSE #3:END
1060 IF MID$(A$,A,B)>MID$(B$,A,B) THEN GOTO 1110
1070 PRINT #3,A$:RC=RC+1
1080 IF EOF(1) THEN GOTO 1150
1090 LINE INPUT #1,A$
1100 GOTO 1060
1110 PRINT #3,B$:RC=RC+1
1120 IF EOF(2) THEN GOTO 1200
1130 LINE INPUT #2,B$
1140 GOTO 1060
1150 CLOSE #1
1160 PRINT #3,B$:RC=RC+1
1170 IF EOF(2) THEN CLOSE #3:CLOSE #2:GOTO 1250
1180 LINE INPUT #2,B$
1190 GOTO 1160
1200 CLOSE #2
1210 PRINT #3,A$:RC=RC+1
1220 IF EOF(1) THEN CLOSE #3:CLOSE #1:GOTO 1250
1230 LINE INPUT #1,A$
1240 GOTO 1210
1250 PRINT "MERGE ";MF;" COMPLETE "
1260 KILL "101.MFL"
1270 KILL MFIL2$
1280 NAME "N101.MFL" AS "101.MFL"
1290 IF CH=N THEN GOTO 1350
1300 RC=0
1310 CH=VAL(MID$(MFIL2$,1,3))+1
1320 MF=CH
1330 MID$(MFIL2$,1,3)=MID$(STR$(CH),2,3)
1340 GOTO 960
1350 PRINT "FINAL  MERGE COMPLETED "
1360 PRINT "RECORDS PROCESSED = ";RC
1370 CLOSE
1380 NAME "101.MFL" AS NW$
1390 PRINT "NEW FILE NAME = ";NW$
1400 END
 "
1360 PRINT "RECORDS PROCESSED = ";RC
1370 CLOSE
1380 NAME "101.MFL" AS NW$
1390 PRI30 NFIL$=MID$(STR$(FCNT),2,3)+".MFL" 'INCREMENTS NUMBER
740 OPEN "O",#3,NFIL$
750 FOR I=1 TO S 'WRITE LOOP
760 PRINT #3,A$(I