SortFile NLM
(Version 2.10)

Recently as part of a larger software project, we had a need to sort records in text files into either ascending or descending order before processing. A quick look around on the Internet landed us a simple C implementation of a merge sort algorithm by Philip J. Erdelsky. The beauty of this particular implementation is that the function is non-recursive (so there is no problem of stack overflow) and re-entrant.

We simply ported Mr. Erdelsky's code to run as an NLM with a few enhancements to the main() routine (with additional error checking to prevent abending the server, for instance). And to make it a "well-behaved" NLM, calls to ThreadSwitchWithDelay were inserted into loops were applicable. We also increased the maximum record length from the original 255 bytes to 1024 bytes; there is no overall file size limit. (See the original documentation for details.)

The usage syntax for the SortFile NLM is as follows:

LOAD SORTFILE inputFile outputFile [-D] [start col#] [end col#]

where

For example,

LOAD SORTFILE sys:data1.txt vol1:data2.txt -D 1 20

sorts the file data1.txt, located on SYS:, in descending order and placing the output file, data2.txt, on VOL1: The sort key will be based on the first 20 bytes of each record. If the columns are omitted, the whole record is used as the "key".


Since the original code on which this port is based on is public domain, we are releasing this port as freeware. Copy it, distribute it, and use it as you see fit, as long as you do not modify it in any way.

You assume all risk and liability for the use of this module by using it. <wink>