on error goto handle:

filin:
cls

Input "Enter ICES formatted file name for sorting ";INB$
OPEN INB$ as #1 len = 82
ag:
input "Enter file name to store sorted file ";c$
if INB$=c$ then print "beg your pardon, but you cannot call it this":goto ag

beg:
cls
print
print "Sort criteria are (ST)ation number (CO)untry  (SH)ip (YE)ar"
print "                    (MO)nth (DA)y  (HO)ur"
print "   Enter ZZ to finish :  Enter (CA)ncel to start again"
print:print"Sort criteria: "

insel:
PRINT "selection ? ";
s1$=input$(1):print " ";s1$;
s2$=input$(1):print s2$
s$=s1$+s2$
s$=UCASE$(s$)
if s$="ZZ" then pr
if s$="CA" then i=0:goto beg
if s$="ST" or s$="CO" or s$="SH" or s$="YE" or s$="MO" or s$="DA" or s$="HO" then
incr i

srt$(i)=s$
else
print "Invalid selection"
end if
goto insel

'proceed after sort selctions made
pr:
nosel=i
cls
for i = 1 to nosel
select case srt$(i)

case = "ST"
bs(i)=5:be(i)=4
case = "CO"
bs(i)=1:be(i)=2
case = "SH"
bs(i)=3:be(i)=2
case= "YE"
bs(i)=20:be(i)=2
case = "MO"
bs(i)=22:be(i)=2
case = "DA"
bs(i)=24:be(i)=2
case="HO"
bs(i)=26:be(i)=2
end select
next i


open "temp.srt" for output as #3
cno=1
field #1, 82 as a$
cls
locate 10,5:print "create index file"

410 WHILE cno<= lof(1)/82

get #1,cno
if mid$(a$,80,1)="J" or mid$(a$,80,1)="2" then
asrt$=""
for i = 1 to nosel
 asrt$=asrt$+mid$(a$,bs(i),be(i))
if bs(i)=26 then asrt$=asrt$+mid$(a$,69,2)
 next i
cno$=str$(cno)
cno$=right$(cno$,len(cno$)-1)
while len(cno$)<=5
cno$="0"+cno$
wend
asrt$=asrt$+","+cno$
print #3,asrt$
end if
incr cno
wend
linlen=len(asrt$)+2
close #3:close #1

open "temp.srt" as #1 len=linlen

if lof(1) >190000 then
Print "Sorry, cannot procede as file too big"
end
gosub srt 'doesnt work
name "temp.srt" as "temp8.srt"
goto mainot
end if
if lof(1)<60000 then
close #1
locate 10,5:print "sorting..........."
shell "sort <temp.srt >temp8.srt"
kill "temp.srt"
goto mainot
end if

field #1,linlen as a$
cn1=1:cn2=int(lof(1)/(3*linlen))
cn3=cn2+1:
cn4=2*cn2
cn5=cn4+1
cn6=int(lof(1)/linlen)

cno=0
close #1
open "temp.srt" for input as #1
open "temp1.srt" for output as #3
open "temp2.srt" for output as #4
open "temp3.srt" for output as #5

locate 10,5:print "sorting sequence .. stage 1"
WHILE cno<cn6
incr cno
line input #1,a$

select case cno
case < cn2
print #3,a$
case < cn4
print #4,a$
case else
print #5,a$
end select

wend
close #1:kill "temp.srt"
locate 10,5:
print "sorting sequence .. stage 2"
shell "sort <temp1.srt >temp4.srt"
shell "sort <temp2.srt >temp5.srt"
shell "sort <temp3.srt >temp6.srt"
close #2:close #3:close #4:close #5

open "temp4.srt" for input as #1
open "temp5.srt" for input as #2
open "temp7.srt" for output as #3

locate 10,5
print "sorting sequence .. stage 3"

gosub store
if car$<>"" then
if car$<car1$ then print #3,car$:print #3,car1$
if car$>=car1$ then print #3,car1$:print #3,car$
end if
fil1=0:fil2=0:car$="":car1$=""
close #1:close #2:close #3
open "temp6.srt" for input as #1
open "temp7.srt" for input as #2
open "temp8.srt" for output as #3

gosub store

if car$<>"" then
if car$<car1$ then print #3,car$:print #3,car1$
if car$>=car1$ then print #3,car1$:print #3,car$
end if
close #1:close #2:close #3
kill "temp1.srt":kill "temp2.srt":kill "temp3.srt"
kill "temp4.srt":kill "temp5.srt":kill "temp6.srt":kill "temp7.srt"

mainot:
open c$ for output as #2
OPEN INB$ as #1 len = 82

field #1, 82 as a$

open "temp8.srt" for input as #3
go=0
cls
locate 10,5
print "storing to ....... ";c$

while not eof(3)

input #3,aa$,recn
go=0
cyc:
get #1,recn

if mid$(a$,80,1)="J" or mid$(a$,80,1)="2" then incr go
if go=2 then go=0:goto rein
print #2,left$(a$,80)
incr recn
if recn>lof(1)/82 then rein
goto cyc
rein:
wend
close #3
kill "temp8.srt"
locate 10,5: print "Job finished...................."
end

store: 'merging sorted files
gosub lin1:gosub lin2
for i = 1 to cn6
if a$<b$ then
print #3,a$:gosub lin1
if fil1=1 then car$=a$:exit for
else
print #3,b$:gosub lin2
if fil2=1 then car$=b$:exit for
end if
next i
fst1=0
if fil1=1 then
while not eof(2)

if car$<>"" and car$<b$ then print #3,car$:car$="":print #3,b$
if car$<>"" and car$>=b$ then print #3,b$

line input #2,b$
if car$="" then print #3,b$
wend
car1$=b$
else
while not eof(1)
if car$<>"" and car$<a$ then print #3,car$:car$="":print #3,a$
if car$<>"" and car$>=a$ then print #3,a$
'if fst1=0 and car$>"" then print #3,a$:fst1=1:print a$;"A"
line input #1,a$
if car$="" then print #3,a$
wend
car1$=a$
end if
return

lin1:
line input #1,a$
if eof(1) then fil1=1
return
lin2:
line input #2,b$
if eof(2) then fil2=1
return

'general purpose sort - unlimited file size, fixed length record

srt:
'sort file is temp.srt unlimited in size
cls
line input #1,aa$
reclen=len(aa$)+2
close #1
open "temp.srt" for random as #1 len=reclen
field #1, reclen as a$
norec=clng(lof(1)/reclen)

offset=1
locate 10,20
print "Virtual sort routine -- offset ";
locate 10,55: print offset
while offset<norec-1
get #1,offset
a1$=a$:
amin$=a1$

for i = offset+1 to norec
get #1,i
a2$=a$
if amin$>a2$ then
amin$=a2$:min=i
end if
next i

if min>0 then
lset a$=a1$
put #1,min
lset a$=amin$
 put #1,offset
min=0
 end if
offset=offset+1
locate 10,55:print offset
wend
return
handle:
if err=53 then
 print "sorry, file is not accessible "
 print "Press and key to procede, (Q to quit)"
 while not instat
 wend
 s$=inkey$
 if ucase$(s$)="Q" then end
 resume filin
end if
print "error ";err
end