`ooooooooooooooooooooooooo
` Christmas Card Database
`       By OBese87
`ooooooooooooooooooooooooo
 
gosub Setup
gosub CreateArrays
gosub MakePersons
 
`-------------------------
` Main Program
`-------------------------
SortData(LastName)
cls 128
Print "Sorted by surname."
DisplayData()
sync
 
wait key
 
SortData(FirstName)
cls 128
Print "Sorted by first name."
DisplayData()
sync
 
END
 
`-------------------------
` Gosubs
`-------------------------
Setup:
hide mouse
sync on
return
`-----
CreateArrays:
   DIM Person$(100,9) :`Don't use Person$(0)!
   `Label Constants
   LastName       = 1
   FirstName      = 2
   AddressLine1   = 3
   AddressLine2   = 4
   AddressTown    = 5
   AddressCounty  = 6
   PostCode       = 7
   SentCard       = 8
   ReceivedCard   = 9
   DIM Sort(100) :`used to sort entries for display
return
`-----
MakePersons:
   DataMax=7
   Data "Palmer","Luke","2 Hillside","Abbotts Ann","Andover","Hampshire","~","NO","NO"
   Data "Powell","Thomas","151 Bury Hill Close","Anna Valley","Andover","Hampshire","SP11 7LL","NO","NO"
   Data "Spencer","Thomas","22 Winchester Road","","Andover","Hampshire","~","NO","NO"
   Data "Jackson","Laurianne","76 Tavistock Road","~","Fleet","Hampshire","~","NO","NO"
   Data "Burt","Andrew","12 Stoney Court","Stoney Lane","Winchester","Hampshire","~","NO","NO"
   Data "Burto","Andre","12 Stoney Court","Stoney Lane","Winchester","Hampshire","~","NO","NO"
   Data "Spence","Laura","12 Stoney Court","Stoney Lane","Winchester","Hampshire","~","NO","NO"
 
   For e = 1 to DataMax
      For f = 1 to 9
         read Person$(e,f)
      Next f
   Next e
return
 
`-------------------------
` Functions
`-------------------------
Function SortData(field)
   DataMax=7
 
   `Wipe Previous Sort Data
   For clr = 1 to DataMax
      Sort(clr)=0
   Next clr
 
   `Sort Data
   For entity = 1 to DataMax
      For pos = 1 to DataMax-1
         `Is current position empty?
         If Sort(pos)=0 then Sort(pos)=entity : exit
 
         `Check for lower characters
         newc=1
         Repeat
            c=newc
            If ASC(mid$(Person$(entity,field),c)) < ASC(mid$(Person$(Sort(pos),field),c)) or c=Len(Person$(entity,field))
               `shift data to make space for current entity
               For shift = DataMax-1 to pos step -1
                  Sort(shift+1)=Sort(shift)
               Next shift
               `place current entity in Sort array
               Sort(pos)=entity
               `reset for loop for new entity
               pos=DataMax
            Else
               If ASC(mid$(Person$(entity,field),c)) = ASC(mid$(Person$(Sort(pos),field),c)) then newc=c+1
            Endif
         Until newc=c
 
         if pos=DataMax-1 then Sort(DataMax)=entity :`If we can't find a place for it then it belongs at the end
      Next pos
      `+++++++BUG EXPOSER+++++++
      cls 128
      for d = 1 to DataMax
         Print d;".  ";Sort(d);": ";Person$(Sort(d),1) ; ", " ; Person$(Sort(d),2)
      next d
      sync
      wait key
      `-------BUG EXPOSER-------
   Next entity
EndFunction
`-----
Function DisplayData()
   DataMax=10
   `Label Constants
   LastName       = 1
   FirstName      = 2
   AddressLine1   = 3
   AddressLine2   = 4
   AddressTown    = 5
   PostCode       = 6
   SentCard       = 7
   ReceivedCard   = 8
 
   For entity = 1 to DataMax
      Print entity ; ".  " ; Person$(Sort(entity),Lastname) ; ", " ; Person$(Sort(entity),FirstName)
   Next entity
EndFunction