BinarySort Example
Keywords: binarysort fixed length records
Question:
I need:
- A good way to sort a file.
- A faster way of reading a file.
Answer:
So what is the format of the file on the disk?Files with fixed data width files can be sorted almost instantly. You could write the files that way in the first place.
Anything else requires processing to get it into that shape, then it can be sorted.
;Make a test file to sort ; 4 fields ;Field 1 chars 1-20 len 20 ;space ;Field 2 chars 22-25 len 4 ;space ;Field 3 chars 27-30 len 4 ;space ;Field 4 char 32-78 len 47 ;Dummy field 5 79-80 @CRLF ;for this test case fields 1 2 and 4 are identical. Field 3 is a random ;number we will sort on later. ;Create test file fn="c:\temp\test.dat" fnsort="c:\temp\test.sort" handle=FileOpen(fn,"WRITE") f1=strfix("f1f1f1f1"," ",20) f2=strfixleft(555,0,4) f4=strfix("random text"," ",47) for xx=1 to 200 f3=strfixleft(Random(9999),0,4) FileWrite(handle,strcat(f1," ",f2," ",f3," ",f4)) next FileClose(handle) ;Now sort it of field 3 fs=FileSize(fn) bb=BinaryAlloc(fs) BinaryRead(bb,fn) recsize=80 keyoffset=26 ; Field 3 offset 0 based keysize=4 BinarySort(bb,recsize,keyoffset,keysize,@ASCENDING) ; string sort BinaryWrite(bb,fnsort) BinaryFree(bb) Message("ALL","DONED")
Article ID: W14571Filename: BinarySort Example.txt