WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Binary Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

BinarySort Example

Keywords:   binarysort fixed length records

Question:

I need:
  1. A good way to sort a file.
  2. 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:   W14571
Filename:   BinarySort Example.txt
File Created: 2001:02:27:11:55:08
Last Updated: 2001:02:27:11:55:08