Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSort records in a file on disk
DescriptionThis example shows how to sort records in a file on disk in Visual Basic 6. It uses quicksort to sort the records in the file.
Keywordsquicksort, sort, disk
CategoriesAlgorithms, Files and Directories
 
Thanks to Bert Guffens.

This example uses quicksort to sort the records on the disk.


Notes:

Traditionally people use mergesort to sort large files on slow media like tape drives. For the small example you use here, it wouldn't make much difference. But for such a small problem the real solution is to load the list into memory and sort it there anyway.

For a really large list, for example 10,000,000 records, mergesort will be faster. What you can do is load as many records as will fit in memory at once. For example, you might be able to load 100,000 records at a time. Sort the 100,000 record chunks separately. Then use mergesort to recombine them into a sorted list.

For more information on sorting and other algorithms, see my book Ready-to-Run Visual Basic Algorithms.


Additional explanation by Bert:

If you are like me and work with a lot of random access files in a database manner and need your data to be sorted order when this routine is for you.

In the early days, one had to resort to setting up arrays, loading and sorting them and then rewrite the contents out again. This lead to problems where memory was insufficient and multi-dimension was not always possible beyond two.

Now this is a thing of the past, you can sort hundreds of megabyte if needed ON DISK within the same file and extremely fast. Just about the only thing you need to watch is the structure of your TYPE, the primary field you want to sort on should be at the beginning of the TYPE.

Example:

    Type Addresses
        LastName as string * 25
        FirstName as string * 20
        Street as string * 25
        Number as integer
        Zip as string * 8
        Town as string * 20
        Tel as string * 13
        EMail as string * 25
    End Type

(Dim AD as Addresses) (LenOfRecord = 138)

In this case the LastName is the primary sort field followed by the first name. If you want the town or the Zip code to be the secondary field to be considered for the sort adjust your TYPE accordingly.

There is practically no limit to the number of fields or the entire length of a record, just take care to set LenOfRecord exactly to the total number of bytes specified in the TYPE taking into consideration the length of other variables than strings (integer = 2 bytes etc.)

[Hint: Define a variable of this type and use Len as in:

    Dim test As Addresses

the_length = Len(test)

Then you don't need to risk messing up this calculation. -- Rod]

Remember that results are always proportional to the care you took at design time. The source code is heavily documented and any additional information is gladly provided by E-Mail.

If you use the code, which is freeware, I would appreciate if you let me know about. Enjoy.


The algorithm itself is a fairly straightfoward quicksort. See the code for details.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated